linking with a pragma with g++

2019-02-07 03:18发布

问题:

In Visual C++, one may link to a library in the code itself by doing #pragma comment (lib, "libname.lib"). Is something similar possible in g++?

回答1:

The Boost Config library has some support for autolinking, using the relevant compiler-specific code for the particular compiler. However, the docs note that the GCC toolchain doesn't support autolinking:

Auto-Linking

Most Windows compilers and linkers have so-called “auto-linking support,” which eliminates the second challenge. Special code in Boost header files detects your compiler options and uses that information to encode the name of the correct library into your object files; the linker selects the library with that name from the directories you've told it to search.

The GCC toolchains (Cygwin and MinGW) are notable exceptions; GCC users should refer to the linking instructions for Unix variant OSes for the appropriate command-line options to use.