Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

-Werror=unknown-pragmas

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top