C++ Visual Studio:使用 pragma 注释链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12821391/
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-27 16:38:58  来源:igfitidea点击:

C++ Visual Studio: linking using pragma comment

c++visual-studio-2010static-linkingdynamic-linking

提问by user1612986

I came across a piece of code which uses #pragma comment(lib, "libraryname").

我遇到了一段使用#pragma comment(lib, "libraryname") 的代码。

Why this type of usage as opposed to just linking the library from the properties menu? In what situations is this usage called for? I am in windows using C++ Visual Studio 2010.

为什么要使用这种类型的用法而不是仅从属性菜单链接库?在什么情况下需要这种用法?我在 Windows 中使用 C++ Visual Studio 2010

It would be nice to see an example which calls for this type of usage.

很高兴看到一个需要这种用法的示例。

回答by Alex F

The library writer can place a #pragma comment(lib, ...)command in the public header (.h) file. In this case, the client doesn't need to add this library to the linker dependencies list. By including an h-file in the program, the client is automatically linked to the required library.

库编写器可以#pragma comment(lib, ...)在公共头 (.h) 文件中放置命令。在这种情况下,客户端不需要将此库添加到链接器依赖项列表中。通过在程序中包含一个 h 文件,客户端会自动链接到所需的库。

回答by Luchian Grigore

Classic example - linking against different versions of the library:

经典示例 - 链接不同版本的库:

#if CURRENT_VERSION >= 10
     #pragma comment(lib, "thirdPartyLibV2.0.lib")
#else //version < 10
     #pragma comment(lib, "thirdPartyLibV1.0.lib")
#endif

回答by Blindy

It's contained in the sense that all it takes is including the header file for the associated library to be automatically pulled in. You can even do #ifdef..#endifmagic to conditionally bring in the right library based on your environment.

从某种意义上说,它所包含的所有内容都包括要自动引入的关联库的头文件。您甚至可以#ifdef..#endif根据环境有条件地引入正确的库。

Not everyone is going to be using your MSVC project when starting a new project from scratch, simply being able to #includeand have it work is the sign of a well written library.

当从头开始一个新项目时,并不是每个人都会使用你的 MSVC 项目,仅仅能够#include并让它工作就是一个写得很好的库的标志。