Several precautions for creating a C/C ++ dynamic link library (dll), dynamic link library dll

Source: Internet
Author: User

Several precautions for creating a C/C ++ dynamic link library (dll), dynamic link library dll
I. The C \ C ++ Runtime Library compilation option is a simple explanation: My dll cannot be used by others.

The Runtime Library is a very complex thing. It is a part of the dll production needs to be understood during the development process. Here we will briefly introduce how to select the compilation option.

In our development process, we often encounter the following problems:

1. My VS version is relatively high (for example, VS2012). I want to create a dll and encapsulate several functions for others.

2. After packaging, I found that my dll references msvcr110.dll or msvcr110d. dll, which may not be available on other computers.

3. When someone else uses it, there are problems such as: "cannot find the" XXXX () "entry point in DLL" XXXX. dll.

The final result is that no error is found during repeated checks. You can use a tool to check whether the function has been exported, but no one else can use it.

Here, you may need to modify the compilation options.

Explanation: how to avoid the above problems

Open Project Properties> Configuration Properties> C/C ++> code generation> runtime in. Multiple options are displayed, as shown in:

Option Description /MD

Make the application use the multi-thread of the Runtime Library and the DLL-specific version. Definition_ MTAnd_ DLLAnd make the compiler put the library name MSVCRT. lib into the. obj file.

The application compiled with this option is statically linked to MSVCRT. lib. This library provides a code layer that allows the linker to parse external references. The actual working code is included in MSVCR80.DLL. This library must be available for applications linked to MSVCRT. lib at runtime.

When_ STATIC_CPPLIB(/D_STATIC_CPPLIB)./MDIt will lead to applications and static multi-threaded Standard C ++ Library (libcpmt. lib) instead of the dynamic version (msvcprt. lib) Link, still through msvcrt. lib is dynamically linked to the master CRT.

/MDd Definition _ DEBUG, _ MTAnd _ DLLAnd the application uses the multi-thread debugging of the Runtime Library and is specific to the DLL version. It also enables the compiler to put the library name MSVCRTD. lib into the. obj file. /MT Make the application use the multi-threaded static version of The Runtime Library. Definition _ MTThe compiler puts the library name LIBCMT. lib into the. obj file so that the linker can use LIBCMT. lib to parse external symbols. /MTd Definition _ DEBUGAnd _ MT. This option also enables the compiler to put the library name LIBCMTD. lib into the. obj file so that the linker can use LIBCMTD. lib to parse external symbols. /LD

Create a DLL.

Pass the/DLL option to the linker. Find the linkerDllMainFunction, but this function is not required. If noDllMainFunction. The linker insertsDllMainFunction.

Link DLL startup code.

If the Export (. exp) file is not specified on the command line, create the import/export (. lib); link the import/export link to the application that calls your DLL.

The/Fe (named EXE file) is interpreted as the Named DLL instead of the. exe file; the default program name isBasename. Dll insteadBasename. Exe.

Unless explicitly specified/MDOtherwise, it indicates/MT.

/LDd

Create a debug DLL. Definition_ MTAnd_ DEBUG.

From the perspective of VS dll library compilation options, the first four items are/MD,/MDd,/MT, And/MTd. Among them, "d" after/MDd and/MTd indicates that the compilation generates the Debug version, that is, the program used for compiling the Debug version; without "d", the program is the Release version, if you configure the project to the Debug version, the compilation will fail.

Dynamic Link multi-threaded Library (MD/MDd)

Dynamic Link to the Runtime Library. In this case, msvcrt. lib is placed in the obj file. It connects to the dll through static link. In fact, the working library is msvcrxx. dll. All C-library functions are stored in the dynamic link library msvcrXX. dll, which handles multithreading issues. That is to say, in this compilation method, we use the dynamic link library msvcrXX. dll to link the CRT.

In this case, the compiled dll references the msvcrXX. dll file, which must be queried by the computer during use. For example, if msvcr110.dll is referenced in my computer compilation, the other computer must have msvcr110.dll when calling the dll to others. Otherwise, the dll library cannot be used. Therefore, msvcr110.dll must be carried together and registered on the other computer.

When using this method to compile, use Depends to view. We can see that the dll references msvcr110.dll, for example:

 

# Define SW_REV extern "C" _ declspec (dllexport) SW_REV int _ stdcall add (int a, int B); int _ stdcall add (int a, int B) {return a + B ;}

 

_ Cdecl call

_ Cdecl is the default call method of C/C ++. The parameter uses the pressure stack mode from right to left. The memory stack of the transfer parameter is maintained by the caller. _ Functions agreed by cedcl can only be called by C/C ++. Each function that calls it contains code for clearing the stack, therefore, the size of the executable file is larger than that of the _ stdcall function.

Without modification, VC ++ uses this call method by default in the following format (default, can be omitted ):

#define SW_REV extern "C" _declspec(dllexport)SW_REV int add(int a, int b);int add(int a, int b){    return a+b;}
When to differentiate?

In fact, both methods are the same, but the difference is that the call. For example, if some languages require that you only recognize _ stdcall for calling, you can only use the _ stdcall version of dll as required. In many cases, the call method can be selected. To put it bluntly, it must be consistent. For example, C # Calls the above _ stdcall dll:

[DllImport("SwLib.dll", EntryPoint = "add", CallingConvention = CallingConvention.StdCall)]        private static extern int add(int a, int b);

Here, you need to select CallingConvention. StdCall for CallingConvention. if I change it to CallingConvention. Cdecl, the program will report an error, indicating that the stack call is asymmetrical:

 

PS: It's really hard to write this thing. It's been a long time since I wrote it last time. There are a lot of things I want to record, and I found myself too lazy to write, so I'm so lazy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.