GCC或G++在编译链接时,如果命令行中含有库,则要特别注意了。根据《C专家编程》5.3节中的提示,GCC在链接时对命令行时的处理顺序是从左到右。证据是GCC的MAN:

-l library
Search the library named library when linking. (The second alter-
native with the library as a separate argument is only for POSIX
compliance and is not recommended.)

It makes a difference where in the command you write this option;
the linker searches and processes libraries and object files in the
order they are specified. Thus, foo.o -lz bar.o searches library z
after file foo.o but before bar.o. If bar.o refers to functions in
z, those functions may not be loaded
.

比如,在测试下面例子时:

gcc -o program program.o libfoo.a

gcc -o program libfoo.a program.o

继续阅读