想请教下,为什么在Makefile中其放在目标文件后边使用的时候没有问题,为什么放在目标文件前边就提示要使用的静态库
或者共享库中的函数不存在
前提libadd.a是存在的
比如编译生成main程序:
gcc -L 库文件所在路径 -ladd main.o -o main
这样使用提示我要体用libadd.a中的add函数未引用
如果我将-ladd 放在main的后边就不报错
gcc的连接选项是有顺序的,应该将最被依赖的选项放到最后,这样才不容易出现undefined reference to xx
之类的错误。经验是将-l开头的选项放到命令行的末尾,这样不容易出问题
这是跟GCC的自己的bug
The problem isn't really with your command line - it's a problem with GNU ld. GNU ld, upon encountering an -lXXX option, reads all the symbols defined by libXXX.so or libXXX.a, checks if any of those symbols have been mentioned in files that have been specified before the -lXXX option in the command line, and forgets about the rest of the symbols.
In other words: command line order of object files and libraries changes behavior of the linker.