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

3

67

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?

Irrelevant answered 6/11, 2009 at 3:20 Comment(1)
BTW, there was a feature request for this: gcc.gnu.org/bugzilla/show_bug.cgi?id=39437Darell
O
31

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.

Outstretch answered 6/11, 2009 at 3:35 Comment(3)
Same applies for some, if not all Windows compilers. But yes, such pragmas are badFlagg
"correct compilation depends on link order." -- No, as the linked article states, correct LINKING depends on link order.Looksee
Depending on the order is sometimes a good thing for the linking, but it should not disallow specifying something actually not depending on any specific linking order (except the order to the system libraries which always come later). In particular, it is just stupid to rule out the cases with only one external library for this reason. It is generally unsound for C/C++ folks as it is like to say, "hey, we don't like undefined/unspecified behavior, so let's make all of them well-defined!"Leonerd
W
21

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.

Waddington answered 26/11, 2010 at 12:23 Comment(2)
You are not entirely right, there are some cases like using ROOT (root.cern.ch) where this could be very helpful.Mineralize
OP clearly figured that much already, the GCC linker doesn't understand that pragma. This in no way answers the question.Dimeter
E
0

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

-Werror=unknown-pragmas

Eloquence answered 21/9, 2018 at 21:3 Comment(1)
...and no, GCC 9.2 or clang 8 still do not understand this pragma (e.g. warning: ignoring #pragma comment [-Wunknown-pragmas] \\ #pragma comment(lib,"xxx.lib"))Kezer

© 2022 - 2024 — McMap. All rights reserved.