C++:#pragma comment(lib, "XXX") 实际上对 "XXX" 有什么作用?

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

C++: What does #pragma comment(lib, "XXX") actually do with "XXX"?

c++pragma

提问by steglig

My background is C# but I have to maintain some legacy (MS) C++. In that codebase I stumpled across:

我的背景是 C#,但我必须维护一些遗留 (MS) C++。在那个代码库中,我偶然发现:

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

where 700 is some versioning. Besides the lib is a DLL with the same name.

其中 700 是某种版本控制。除了lib是一个同名的DLL。

I first thought that the program would be dependant upon the DLL but after removing it from the system the program still works. There exists a newer version of the DLL, though, which is named OtherLib900...

我首先认为该程序将依赖于 DLL,但在将其从系统中删除后,该程序仍然可以运行。但是,存在一个较新版本的 DLL,名为 OtherLib900...

It seems as though the program 'included' the code of the lib so that it's no longer dependant upon the external DLL. (Or that the program 'automatically' uses the newer DLL...)

程序似乎“包含”了 lib 的代码,因此它不再依赖于外部 DLL。(或者程序“自动”使用较新的 DLL ......)

Which one is correct? Is there are way to further confirm that 'assumption'?

哪一个是正确的?有没有办法进一步证实这个“假设”?

采纳答案by drescherjm

If a program has this pragma it will look for the library OtherLib700.lib. If that is an import library when the program is loaded windows will search for OtherLib700.dllin the path. It will not try to look for OtherLib900.dllduring execution so it must be finding your dllin a different folder. This assumes that OtherLib700.libis an import library and not a static library. If OtherLib700.lib is a static library then that is all it needs.

如果程序有这个 pragma,它会查找库OtherLib700.lib。如果这是加载程序时的导入库,Windows 将OtherLib700.dll在路径中搜索。它不会OtherLib900.dll在执行期间尝试查找,因此它必须dll在不同的文件夹中找到您。这假设它OtherLib700.lib是一个导入库而不是一个静态库。如果 OtherLib700.lib 是一个静态库,那么这就是它所需要的。

回答by Luchian Grigore

That pragmais used to link against the specified .libfile. This is an alternative to specifying the library in the external dependencies field in project settings.

pragma用于链接到指定的.lib文件。这是在项目设置的外部依赖项字段中指定库的替代方法。

Mostly, it's used to support different versions:

大多数情况下,它用于支持不同的版本:

#ifdef USE_FIRST_VERSION
#pragma comment(lib, "vers1.lib")
#else
#pragma comment(lib, "vers2.lib")
#endif

When your application uses a dynamically-linked library, a libfile tells you information about what symbols are exported in the dll. So basically you only need the libto compile & link, but you need the dllto run the program, as it contains all the binary code.

当您的应用程序使用动态链接库时,一个lib文件会告诉您有关在dll. 所以基本上你只需要lib编译和链接,但你需要dll运行程序,因为它包含所有的二进制代码。

You say there's an associated dll, which usually indicates the libfile only contains linking info, and no code. You should get a run-time error if the associated dllwas not found. You can check with MSVS if a different version of the dllwas loaded or if it was loaded from a different place.

你说有一个关联的dll,这通常表明lib文件只包含链接信息,没有代码。如果dll未找到关联,您应该会收到运行时错误。您可以使用 MSVS 检查是否dll加载了不同版本的或者是否从其他位置加载。

回答by Christian Stieber

If the .lib is a "real" lib with the actual code (I've never used DLLs save for the system-provided ones, but I believe you make 'import libs' for your own DLLs), then the DLL isn't required.

如果 .lib 是带有实际代码的“真实”库(我从未使用过系统提供的 DLL,但我相信您为自己的 DLL 制作了“导入库”),那么 DLL 就不是必需的。

As for the subject, #pragma comment(lib,xxx) allows programs to add certain options for the linker. Can be very useful, although I've missed a few options that I would have liked to add like this. The example given is a prime example for its use: when the object file is included in the program, then the lib specified will be added as well.

至于主题,#pragma comment(lib,xxx) 允许程序为链接器添加某些选项。可能非常有用,尽管我错过了一些我希望像这样添加的选项。给出的示例是其使用的主要示例:当目标文件包含在程序中时,指定的 lib 也将被添加。