Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the exact steps for creating and then linking against a Win32 DLL on the command line?

Here's my library Lib.c file:

#include <stdio.h>

int helloworld(){
    printf("Hello World DLL");
}

Here's 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 against Lib.dll.

What are the specific steps to achieve this?

like image 429
merlin2011 Avatar asked Jan 27 '12 16:01

merlin2011


1 Answers

See this related question on how to build the DLL.

Your library code as it stands does not export any symbols and your executable does not import the symbols from your library. Two typical patterns for doing that are shown below but you might want to read up on that first.

The first method uses __declspec() to declare in the code what functions (or other items) are exported from your DLL and imported by other executables. You use a header file to declare the exported items and have a preprocessor flag used to control whether the symbols are exports or imports:

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 have also specifically set the calling convention to __stdcall as are most DLL functions (I could have used WINAPI instead of __stdcall if I had included windows.h) and have declared the functions as extern "C" so their names do not get mangled when compiled as C++. Not such a problem here as it's all C, but if you were to build the DLL from C source and then try to use it from a C++ executable then the imported names would be incorrect.

The code could then look like this:

mylib.c

#include "mylib.h"
#include <stdio.h>

int MYLIB_API helloworld(void)
{
    printf("Hello World DLL");
    return 42;
}

You'd build your DLL using the following command line. As well as creating the DLL it will create the import library (.lib) required to use your DLL from another executable (as well as the export file, but that is only required in certain circumstances):

cl /DBUILDING_MYLIB mylib.c /LD

The /DBUILDING_MYLIB argument defines the preprocessor symbol used to control whether the functions in the DLL are exports (if it is defined) or imports (not defined). So you'd define it when building the DLL but not when building your application.

The /LD parameter tells cl to produce a DLL.

The second method is to use module definition files as mentioned in the comments. You can use the code you already have but you also need to create the module definition file. At it's simplest it looks like this:

LIBRARY   mylib
EXPORTS
   helloworld

In this case to build the DLL you require the following command line:

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

You could then code your application so that it used your 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);
}

Which you could then compile with the following command line (assuming the import library from the DLL creation is in the same directory as your main.c). This step is the same whether you used declspec or module definition files:

cl main.c /link mylib.lib

Arguments passed after the /link argument are passed onto the linker command line as they appear, so as just a filename it is used as extra input to link into the executable. In this case we specify the import library generated when we built the DLL.

The command lines I've shown here are pretty much the absolute minimum you'd need but it'll allow you to create a DLL and link an application to it.

I have assumed the calling convention is correct in all of the above and I have not experimented much to see whether I got it wrong at any point.

like image 123
tinman Avatar answered Oct 21 '22 13:10

tinman