什么是创造,然后在命令行上链接到的一个Win32 DLL的具体步骤? [英] What are the exact steps for creating and then linking against a Win32 DLL on the command line?

查看:138
本文介绍了什么是创造,然后在命令行上链接到的一个Win32 DLL的具体步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的库 Lib.c 文件:

#include <stdio.h>

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

下面是我的exe MAIN.C 文件:

Here's my exe Main.c file:

int helloworld();


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

我想创建 Lib.dll MAIN.EXE ,其中 Lib.dll 来自 Lib.c MAIN.EXE 链接对 Lib.dll

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?

推荐答案

请参阅this关于如何建立该DLL相关的问题。

See this related question on how to build the DLL.

您库code,因为它代表不出口任何符号和可执行文件不会从资料库中导入的符号。做两种典型模式,如下图所示,但你可能要上宣读了第一位。

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.

第一种方法使用 __ declspec()在code从DLL导出什么函数(或其他物品)和其他可执行进口申报。您可以使用头文件来声明导出的项目,并有用于控制符号是否出口或进口preprocessor标志:

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

我还专门调用约定设置为 __ STDCALL 作为最DLL函数(我可以用 WINAPI 而不是 __ STDCALL ,如果我已经包含WINDOWS.H),并宣布作为的externC所以他们的名字当作为C ++编译不要错位。不会出现这样的问题,在这里,因为它是所有C,但是如果你要建立从C源代码的DLL,然后尝试在C ++中使用的可执行文件,然后导入的名字是不正确的。

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.

然后code可能是这样的:

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;
}

您会使用以下命令行建立你的DLL。以及创造它会创建使用您的DLL从另一个可执行文件(以及导出文件所需的导入库(.LIB)的DLL,但仅在的某些情况下中):

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

/ DBUILDING_MYLIB 参数定义了用于控制DLL中的函数是出口(如果它被定义)preprocessor符号或进口(没有定义) 。所以,你会生成DLL而不是构建应用程序时,当定义它。

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.

/ LD 参数告诉CL产生的DLL。

The /LD parameter tells cl to produce a DLL.

的第二种方法是使用模块定义文件在评论中提到。你可以用code你已经有了,但你还需要创建模块定义文件。在它的简单,它看起来是这样的:

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

在这种情况下,建立你需要以下命令行DLL:

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

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

您可以据此code您的应用程序,使其使用你的库的头与你的DLL函数导入版本:

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);
}

然后你可以用下面的命令行编译(从DLL创建假设导入库是在相同的目录main.c中)。这个步骤是一样的你是否使用declspec或模块定义文件:

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

/链接参数通过后的参数传递到链接器命令行,因为它们出现,所以只是一个文件名,它被用作额外的输入链接到可执行文件。在这种情况下,我们指定,当我们建立的DLL产生的导入库。

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.

我在这里显示的命令行是pretty多的绝对最低你需要,但它会允许您创建一个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.

这篇关于什么是创造,然后在命令行上链接到的一个Win32 DLL的具体步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆