C++ 在 Visual Studio 中链接 dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7845886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:35:27  来源:igfitidea点击:

Linking dll in Visual Studio

c++visual-studio

提问by smallB

How can I add a .dllin Visual Studio 2010? I just cannot find the option there.

如何.dll在 Visual Studio 2010 中添加一个?我只是在那里找不到选项。

回答by gwiazdorrr

On Windows you do not link with a .dllfile directly – you must use the accompanying .libfile instead. To do that go to Project -> Properties -> Configuration Properties -> Linker -> Additional Dependenciesand add path to your .lib as a next line.

在 Windows 上,您不.dll直接与文件链接——您必须改用随附的.lib文件。要做到这一点,请Project -> Properties -> Configuration Properties -> Linker -> Additional Dependencies在下一行添加到 .lib 的路径。

You also mustmake sure that the .dllfile is either in the directory contained by the %PATH%environment variable or that its copy is in Output Directory(by default, this is Debug\Releaseunder your project's folder).

您还必须确保该.dll文件位于%PATH%环境变量包含的目录中或其副本位于其中Output Directory(默认情况下,它位于 Debug\Release您的项目文件夹下)。

If you don't have access to the .libfile, one alternative is to load the .dllmanually during runtime using WINAPI functions such as LoadLibraryand GetProcAddress.

如果您无权访问该.lib文件,另一种方法是.dll在运行时使用 WINAPI 函数(如LoadLibraryGetProcAddress )手动加载。

回答by ssube

You don't add or link directly against a DLL, you link against the LIB produced by the DLL.

您不直接添加或链接到 DLL,而是链接到由 DLL 生成的 LIB。

A LIB provides symbols and other necessary data to either include a library in your code (static linking) or refer to the DLL (dynamic linking).

LIB 提供符号和其他必要数据,以便在代码中包含库(静态链接)或引用 DLL(动态链接)。

To link against a LIB, you need to add it to the project Properties -> Linker -> Input -> Additional Dependencies list. All LIB files here will be used in linking. You can also use a pragma like so:

要链接 LIB,您需要将其添加到项目 Properties -> Linker -> Input -> Additional Dependencies 列表中。这里的所有 LIB 文件都将用于链接。您还可以使用这样的编译指示:

#pragma comment(lib, "dll.lib")

With static linking, the code is included in your executable and there are no runtime dependencies. Dynamic linking requires a DLL with matching name and symbols be available within the search path(which is notjust the path or system directory).

使用静态链接,代码包含在您的可执行文件中,并且没有运行时依赖项。动态链接需要有匹配的名称和符号的DLL是中可用的搜索路径(这是不是只是路径或系统目录)。

回答by cmc

I find it useful to understand the underlying tools. These are cl.exe (compiler) and link.exe (linker). You need to tell the compiler the signatures of the functions you want to call in the dynamic library (by including the library's header) and you need to tell the linker what the library is called and how to call it (by including the "implib" or import library).

我发现了解底层工具很有用。它们是 cl.exe(编译器)和 link.exe(链接器)。您需要告诉编译器您要在动态库中调用的函数的签名(通过包含库的头文件),并且您需要告诉链接器该库的名称以及如何调用它(通过包含“implib”或导入库)。

This is roughly the same process gcc uses for linking to dynamic libraries on *nix, only the library object file differs.

这与 gcc 用于链接到 *nix 上的动态库的过程大致相同,只是库对象文件不同。

Knowing the underlying tools means you can more quickly find the appropriate settings in the IDE and allows you to check that the commandlines generated are correct.

了解底层工具意味着您可以更快地在 IDE 中找到合适的设置,并允许您检查生成的命令行是否正确。

Example

例子

Say A.exe depends B.dll. You need to include B's header in A.cpp (#include "B.h") then compile and link with B.lib:

说 A.exe 依赖 B.dll。您需要在 A.cpp ( #include "B.h") 中包含 B 的头文件,然后编译并与 B.lib 链接:

cl A.cpp /c /EHsc
link A.obj B.lib

The first line generates A.obj, the second generates A.exe. The /cflag tells cl not to link and /EHscspecifies what kind of C++ exception handling the binary should use (there's no default, so you have to specify something).

第一行生成 A.obj,第二行生成 A.exe。该/c标志告诉 cl 不要链接并/EHsc指定二进制文件应使用哪种 C++ 异常处理(没有默认值,因此您必须指定某些内容)。

If you don't specify /ccl will call linkfor you. You can use the /linkflag to specify additional arguments to linkand do it all at once if you like:

如果您不指定/ccl 会link为您打电话。如果您愿意,您可以使用该/link标志来指定其他参数link并一次性完成所有操作:

cl A.cpp /EHsc /link B.lib

If B.lib is not on the INCLUDEpath you can give a relative or absolute path to it or add its parent directory to your include path with the /Iflag.

如果 B.lib 不在INCLUDE路径上,您可以给它一个相对或绝对路径,或者将其父目录添加到带有/I标志的包含路径中。

If you're calling from cygwin (as I do) replace the forward slashes with dashes.

如果你从 cygwin 调用(就像我一样)用破折号替换正斜杠。

If you write #pragma comment(lib, "B.lib")in A.cpp you're just telling the compiler to leave a comment in A.obj telling the linker to link to B.lib. It's equivalent to specifying B.lib on the link commandline.

如果你用#pragma comment(lib, "B.lib")A.cpp编写,你只是告诉编译器在 A.obj 中留下注释,告诉链接器链接到 B.lib。它相当于在链接命令行上指定 B.lib。