gcc/g++ 参数顺序 [英] gcc/g++ parameter order
问题描述
我刚刚在我的新 ubuntu 12.10 服务器上编译了 chironfs 并收到以下错误:
i just compiled chironfs on my new ubuntu 12.10 server and got the following error:
gcc -Wall -W -Wmissing-prototypes -g -O2 -DFUSE_USE_VERSION=25 -D_FILE_OFFSET_BITS=64 -I/usr/local/include -g -O2 -lm -lfuse -o chironfs chironfs.o chiron-conf.o chirondbg.o chironfn.o
chironfs.o: In function `chiron_init':
/root/chironfs-1.1.1/src/chironfs.c:2000: undefined reference to `pthread_create'
chironfs.o: In function `get_rights_by_name':
/root/chironfs-1.1.1/src/chironfs.c:452: undefined reference to `fuse_get_context'
pthread 错误告诉我缺少 -lpthread,但保险丝错误有点奇怪,因为正在使用 -lfuse
the pthread error tells me that -lpthread is missing, but the fuse error is kinda weird cause -lfuse is being used
我在这里找到了一个解决方案 建议将库放在目标文件之后
i found a solution here which suggests to put libraries after object files
所以我删除了 -lfuse 并在行的最后添加了 -lfuse -lpthread
so i removed -lfuse and added -lfuse -lpthread at the very end of the line
现在它编译没有错误,看起来应该是这样:目标文件之后的库
now it compiles without an error and it seems that this is the way it should be: library after object files
我的问题是:为什么参数顺序与 gcc/ld 相关?我认为 gcc 只是像其他所有应用程序一样解析参数,并且可能会将必要的参数转发给 ld 之类的
my question is: why is the parameter order relevant for gcc/ld? i tought gcc just parses the params like every other application and may forward the necessary params to ld or such
一般来说:任何人都知道 gcc 参数排序的事实或技巧,也许还有一点背景知识关于为什么需要这种方式的信息?
in general: anyone knows facts or tips for gcc parameter ordering and maybe a bit background information about why it is needed this way?
谢谢
推荐答案
对象和库的顺序与链接器有关(在创建可执行文件时由编译器隐式调用).当链接器在从左到右的扫描中发现一个它不知道的名称的使用时,它开始寻找定义从那时起.如果定义通过,它不会记住它以备后用.
The order of objects and libraries is relevant to the linker (called implicitly by the compiler when creating an executable). When the linker, in its left-to-right scan, finds a use of a name it doesn't know about it starts looking for a definition from that point on. If a definition passes by, it doesn't remember it for later use.
这篇关于gcc/g++ 参数顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!