GCC can get pretty picky about the order in which it accepts its arguments:
# Works.
g++ Foo.cpp -L. -I. -lBar -o Foo
# Linker errors.
g++ -o Foo -I. -L. -lBar Foo.cpp
What, specifically, are the ordering requirements for command-line options?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Libraries are loaded on demand based on the symbols required from them, so the library which provides a symbol needed by something else must follow that something else. This is historical; arguably a modern system should resolve symbols automatically, handling loops sensibly (that being the reason for the rule; you broke dependency cycles manually by specifying libraries in order and as many times as needed), but
g++follows the traditional rule so it will work with vendorlds. (GNUlddoesn’t work everywhere, so it wouldn’t be possible to rely on it to resolve symbol dependency loops. There are also bootstrapping concerns even on platforms where GNUlddoes work.) Similarly, other linker-oriented options must be specified in the correct order relative to the things they affect (for example, a-Loption must precede a library which lives in the specified directory; this can be important if a library in one directory shadows a library of the same name in a standard directory).