Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC Command-Line Argument Pickiness

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?

like image 431
Maxpm Avatar asked Jun 06 '11 05:06

Maxpm


1 Answers

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 vendor lds. (GNU ld doesn'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 GNU ld does work.) Similarly, other linker-oriented options must be specified in the correct order relative to the things they affect (for example, a -L option 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).

like image 143
geekosaur Avatar answered Dec 03 '22 20:12

geekosaur