#pragma comment(lib, “xxx.lib”) equivalent under Linux?

三世轮回 提交于 2019-12-17 05:05:11

问题


I have a static library file called libunp.a, I do know I could use gcc -lunp xx to link to the library.

I could use #pragma comment(lib,"xxx.lib") to tell the Microsoft C/C++ compiler to include the library; how could I do it under Linux/GCC?


回答1:


Simple; you can't. GCC has no such equivalent. Specify -l as a gcc parameter, create a linker script, call ld, call 911 or whatever.

Not that such a pragma even makes sense. Libraries should be specified during the linking step. Such information simply doesn't belong inside a translation unit. A translation unit can be preprocessed, compiled and assembled even without a linking stage. The toolchain used by Visual Studio allows this because it is braindead and always performs linking.

You might want to save yourself some tedious typing and create a MakeFile for your project: GNU Make Manual




回答2:


There doesn't seem to be any mention of any equivalent pragmas in the GCC manual's page on pragmas.

One reason I saw for GCC not supporting linking in source code was that sometimes, correct linking depends on link order; and this would require you to make sure that the linking order happens correctly no matter the order of compilation. If you're going to go to that much work, you may as well just pass the linker arguments on the command line (or otherwise), I suppose.




回答3:


Libraries should be specified during the linking step. Such information simply doesn't belong inside a translation unit. A translation unit can be preprocessed, compiled and assembled even without a linking stage.

Simply because #pragma comment(lib,"xxx.lib") is in the source file does not mean the compiler consumes it. In fact, it goes in as a comment and is subsequently used by the linker. Not much different than *nix.




回答4:


Use this GCC flag to generate an error for unknown pragmas. It will quickly tell you if the compiler understands it.

-Werror=unknown-pragmas



来源:https://stackoverflow.com/questions/1685206/pragma-commentlib-xxx-lib-equivalent-under-linux

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