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?
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?
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
warning: ignoring #pragma comment [-Wunknown-pragmas] \\ #pragma comment(lib,"xxx.lib")
) –
Kezer © 2022 - 2024 — McMap. All rights reserved.