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

GccStatic Linking

Gcc Problem Overview


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?

Gcc Solutions


Solution 1 - Gcc

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.

Solution 2 - Gcc

> 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.

Solution 3 - Gcc

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

-Werror=unknown-pragmas

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJichaoView Question on Stackoverflow
Solution 1 - GccMark RushakoffView Answer on Stackoverflow
Solution 2 - GccJeffrey WaltonView Answer on Stackoverflow
Solution 3 - GccJ. WinarskeView Answer on Stackoverflow