您的位置:首页 > Web前端

gcc参数-l传递顺序错误导致`undefined reference'的一点小结

2014-11-04 20:46 274 查看
刚才编译一个pthread的单文件程序, 使用的命令行是:

gcc -o thread1 -lpthread thread1.c

结果报错:

$ gcc -o thread1 -lpthread thread1.c
/tmp/ccNqs6Bh.o: In function `main':
thread1.c:(.text+0x49): undefined reference to `pthread_create'
thread1.c:(.text+0x5f): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status


仔细看了一下, 代码编译过了, 链接的时候出的错. 但pthread库是真实存在的.

而且gcc的语法是:
Usage: gcc [options] file...

是我平常最喜欢的传参方式, 也是按规定的方式传入的, 咋个就不行了呢? 不带这样玩的!

仔细看了看, 发现一个 `-v --help' 的选项, 于是 gcc -v --help, ~!@#$%^&

输出结果将近3000行!!!, 吓尿~~~~~~~

没办法, Google之, 得到了相关说明:
1. Link order of libraries
2. C++ shared library - undefined reference

解决办法:
把对库的引用放在源文件后面, 因为那是传给链接器的参数!

要改成这样:
gcc -o thread1 thread1.c -lpthread

女孩不哭 @ cnblogs.com/memset @ 2014.11.04
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: