C++ Visual Studio: linking using pragma comment

百般思念 提交于 2019-12-01 03:06:04
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.

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

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..#endif magic to conditionally bring in the right library based on your environment.

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

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