简体   繁体   English

C ++:#pragma注释(lib,“ XXX”)实际上对“ XXX”有什么作用?

[英]C++: What does #pragma comment(lib, “XXX”) actually do with “XXX”?

My background is C# but I have to maintain some legacy (MS) C++. 我的背景是C#,但我必须维护一些旧版(MS)C ++。 In that codebase I stumpled across: 在该代码库中,我绊倒了:

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

where 700 is some versioning. 其中700是一些版本。 Besides the lib is a DLL with the same name. 除了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. 我首先认为该程序将依赖于DLL,但是从系统中删除该程序后,该程序仍然可以运行。 There exists a newer version of the DLL, though, which is named OtherLib900... 但是,存在该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. 似乎该程序“包含”了lib的代码,因此它不再依赖于外部DLL。 (Or that the program 'automatically' uses the newer DLL...) (或者该程序“自动”使用更新的DLL ...)

Which one is correct? 哪一个是正确的? Is there are way to further confirm that 'assumption'? 有没有办法进一步确认“假设”?

That pragma is used to link against the specified .lib file. pragma用于链接到指定的.lib文件。 This is an alternative to specifying the library in the external dependencies field in project settings. 这是在项目设置的外部依赖项字段中指定库的替代方法。

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 lib file tells you information about what symbols are exported in the dll . 当您的应用程序使用动态链接的库时, lib文件会告诉您有关dll中导出了哪些符号的信息。 So basically you only need the lib to compile & link, but you need the dll to run the program, as it contains all the binary code. 因此,基本上,您只需要lib进行编译和链接,但是您需要dll来运行程序,因为它包含所有二进制代码。

You say there's an associated dll , which usually indicates the lib file only contains linking info, and no code. 您说有一个关联的dll ,它通常表示lib文件仅包含链接信息,而没有代码。 You should get a run-time error if the associated dll was not found. 如果找不到关联的dll ,您应该会遇到运行时错误。 You can check with MSVS if a different version of the dll was loaded or if it was loaded from a different place. 您可以使用MSVS检查是否加载了其他版本的dll ,或者是否从其他位置加载了它。

If a program has this pragma it will look for the library OtherLib700.lib . 如果程序具有此编译指示,它将寻找库OtherLib700.lib If that is an import library when the program is loaded windows will search for OtherLib700.dll in the path. 如果在导入程序时这是导入库,则Windows将在路径中搜索OtherLib700.dll It will not try to look for OtherLib900.dll during execution so it must be finding your dll in a different folder. 它不会在执行过程中尝试查找OtherLib900.dll ,因此它必须在其他文件夹中找到您的dll This assumes that OtherLib700.lib is an import library and not a static library. 这假定OtherLib700.lib是导入库,而不是静态库。 If OtherLib700.lib is a static library then that is all it needs. 如果OtherLib700.lib是静态库,则只需要它。

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是带有实际代码的“真实” lib(我从未用过DLL来保存系统提供的DLL,但我相信您为自己的DLL制作了“ import libs”),则该DLL不是需要。

As for the subject, #pragma comment(lib,xxx) allows programs to add certain options for the linker. 对于主题,#pragma comment(lib,xxx)允许程序为链接器添加某些选项。 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. 给出的示例是其用法的主要示例:当目标文件包含在程序中时,还将同时添加指定的lib。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM