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

时光毁灭记忆、已成空白 提交于 2019-11-30 01:30:01

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.dll in the path. It will not try to look for OtherLib900.dll during execution so it must be finding your dll in a different folder. This assumes that OtherLib700.lib is an import library and not a static library. If OtherLib700.lib is a static library then that is all it needs.

That pragma is used to link against the specified .lib file. 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. 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.

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!