Selective GCC Command Line Argument

GCC can get a pretty legible order in which it takes its arguments:

# Works. g++ Foo.cpp -L. -I. -lBar -o Foo # Linker errors. g++ -o Foo -I. -L. -lBar Foo.cpp 

What are, in particular, the order requirements for command line options?

+7
source share
1 answer

Libraries are loaded on demand based on the required characters, so a library that provides the character needed by something else must follow something else. It is historical; perhaps the modern system should automatically resolve characters, intelligently handling loops (which is the reason for the rule, you manually scolded dependency loops, specifying libraries in order and as many times as necessary), but g++ follows the traditional rule, so it will work with the ld s provider . (GNU ld does not work everywhere, so it would be impossible to rely on it to resolve character dependency cycles. There are also problems loading even on platforms where GNU ld works.) Likewise, oriented parameters should be specified in the correct order as to what they affect (for example, the -L option must precede the library that lives in the specified directory, this may be important if the library in the same directory shades the library with the same name in the standard directory).

+7
source

All Articles