What are the exact steps to create and then connect to the Win32 DLL on the command line?

Here is my Lib.c file library:

 #include <stdio.h> int helloworld(){ printf("Hello World DLL"); } 

Here is my exe Main.c file:

 int helloworld(); int main(int argc, char** argv){ helloworld(); } 

I would like to create Lib.dll and Main.exe , where Lib.dll comes from Lib.c and Main.exe links from Lib.dll .

What are the concrete steps to achieve this?

+6
source share
1 answer

See this related question on how to create a DLL.

Your library code in it does not export any characters, and your executable file does not import characters from your library. Two typical templates for this are shown below, but you can read about it first.

The first method uses __declspec() to declare in the code which functions (or other elements) are exported from your DLL and imported by other executables. You use the header file to declare exported elements and have a preprocessor flag used to control the export or import of characters:

mylib.h:

 #ifndef MYLIB_H #define MYLIB_H #if defined(BUILDING_MYLIB) #define MYLIB_API __declspec(dllexport) __stdcall #else #define MYLIB_API __declspec(dllimport) __stdcall #endif #ifdef __cplusplus extern "C" { #endif int MYLIB_API helloworld(void); #ifdef __cplusplus } #endif #endif 

I also specifically set the calling convention on __stdcall , like most DLL functions (I could use WINAPI instead of __stdcall if I included windows.h), and declared the functions as extern "C" so their names would not work when compiling like C ++. Not such a problem here, like all C, but if you were to collect the DLL from the C source, and then try to use it from the C ++ executable, then the imported names would be wrong.

Then the code may look like this:

mylib.c

 #include "mylib.h" #include <stdio.h> int MYLIB_API helloworld(void) { printf("Hello World DLL"); return 42; } 

You will create your DLL using the following command line. In addition to creating the DLL, it will create the import library (.lib) needed to use your DLL from another executable file (as well as the export file, but this is only necessary in certain circumstances ):

 cl /DBUILDING_MYLIB mylib.c /LD 

The argument /DBUILDING_MYLIB defines the preprocessor symbol used to control whether the functions in the DLL are exported (if defined) or imported (not defined). Thus, you define it when you create the DLL, but not when you create the application.

The /LD tells cl to create the DLL.

The second way is to use module definition files , as indicated in the comments. You can use the code that you already have, but you also need to create a module definition file. At the same time, it most simply looks like this:

 LIBRARY mylib EXPORTS helloworld 

In this case, the following command line is required to create the DLL:

 cl /LD mylib.c /link /DEF:mylib.def 

You can then encode your application to use the library header with the imported version of your DLL function:

main.c

 /* No need to include this if you went the module definition * route, but you will need to add the function prototype. */ #include "mylib.h" int main(void) { helloworld(); return (0); } 

What could you then compile with the following command line (assuming that the import library from creating the DLL is in the same directory as your main.c). This step is the same if you used declspec or module description files:

 cl main.c /link mylib.lib 

Arguments passed after the /link argument are passed to the linker command line as they appear, since the file name is simply used as an additional input to refer to the executable file. In this case, we specify the import library generated when the DLL was created.

The command lines I showed here are almost the absolute minimum you need, but it will allow you to create a DLL and associate the application with it.

I assumed that the calling convention is correct in all of the above, and I did not experiment much to understand if I was not mistaken at any moment.

+16
source

All Articles