在命令行上,与Win32 DLL链接的确切步骤是什么?[英] What are the exact steps for creating and then linking against a Win32 DLL on the command line?

本文是小编为大家收集整理的关于在命令行上,与Win32 DLL链接的确切步骤是什么?的处理方法,想解了在命令行上,与Win32 DLL链接的确切步骤是什么?的问题怎么解决?在命令行上,与Win32 DLL链接的确切步骤是什么?问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

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?

推荐答案

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.

本文地址:https://www.itbaoku.cn/post/359080.html