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

刚才编译一个pthread的单文件程序, 使用的命令行是:

gcc -o thread1 -lpthread thread1.c

结果报错:

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

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

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

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

仔细看了看, 发现一个 `-v --help‘ 的选项, 于是 gcc -v --help, [email protected]#$%^&

输出结果将近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

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

时间: 2024-08-08 17:56:22

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

由于link顺序错误导致的undefined reference

其实我之前就遇到过这个问题,也强调过,GNU-G++在link阶段是依赖输入的.o或者.a文件的顺序的.如果顺序错误会导致undefined reference错误 见这篇随笔:http://www.cnblogs.com/qrlozte/p/4137704.html 刚才我遇到的问题是什么呢? 代码demo.cpp:其中ZJ::open_max在util.h中声明,在$(PATH_ONE)/libutil.a中:err_sys在apue.h中声明,在$(PATH_TWO)/libapue.a中

GCC编译uboot出现(.ARM.exidx+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'错误的解决办法

/opt/arm-2010.09/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/libgcc.a(_bswapsi2.o):(.ARM.exidx+0x0): undefined reference to `__aeabi_unwind_cpp_pr0' make: *** [u-boot] Error 1 一旦编译uboot出现上述错误,请不要慌张!解决办法官网已经给出,主要解决办法如下: vim 打开./lib_arm/eabi_com

linux 编译错误:undefined reference to `__gxx_personality_v0'

(1)编译 c++ 程序需要添加 -lstdc++ 如: gcc -lstdc++ -o test test.c 否则会报 "undefinedreference to '__gxx_personality_v0' " 错误 (2)用gcc命令编译C程序,用g++命令编译C++程序. g++和gcc本质一样的,本质上还是gcc,我们实验室所有的c++程序都是用gcc编译的,一般的程序用gcc足够了. 对于 C++ 程序,编译的时候用 gcc 或者 g++ 都可以.但是在进行连接的时候最

错误:undefined reference to `__gxx_personality_v0'

使用gcc编译C代码,引用了C++ 库,出现这个错误,网上搜到这哥们的文章,解决问题 转自:错误:undefined reference to `__gxx_personality_v0' 1. Linux Develop Notes * 编译 c++ 程序需要添加 -lstdc++   sample: gcc -lstdc++ -o test test.c,否则会报 "undefinedreference to '__gxx_personality_v0' " 错误 2. 唉,用gc

CentOS 6.7 编译PHP7 make时出现错误:undefined reference to `libiconv_close’

编辑Makefile文件,找到变量EXTRA_LIBS,并在末尾添上-liconv EXTRA_LIBS = -lcrypt -lz -lexslt -lcrypt -lrt -lmcrypt -lltdl -lstdc++ -lpng -lz -ljpeg - lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -lcurl -lxml2 -lz -lm -l ssl -lcrypto -lfreetype -lz

undefined reference to `__gxx_personality_sj0'错误解决办法

在交叉编译一个.cpp库时,出现如下错误: undefined reference to `__gxx_personality_sj0' undefined reference to `operator new[](unsigned int)' 解决办法: 修改库中的Makefile文件,在$(LD)  后面的选项中增加 -lsupc++ undefined reference to `__gxx_personality_sj0'错误解决办法

libmt.so: undefined reference to `[email protected]_53'

[[email protected] instance]# make gcc -O3 -g -I/usr/include/ -I/usr/include/glib-2.0 -I/usr/include/glib-2.0/glib -I/usr/include/glib-2.0/gobject -I/usr/lib/glib-2.0/include -I../../../common -I../../../protocol -I../../../cache -I../../../capture -

(笔记)Linux线程编译undefined reference to 'pthread_create'

在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create' 主要是以下两种原因: 1.#include <pthread.h>  请确认头文件是否添加 2.-lpthread 编译选项,即在编译时需添加额外的编译选项,如使用arm-linux-gcc编译lc300-led-test.c文件,命令正确应该如下: arm-linux-gcc -o lc300-led-test lc300-led-test.

undefined reference to `pthread_create&#39; collect2: ld returned 1 exit status

Linux下编译多线程程序遇到错误: undefined reference to `pthread_create' collect2: ld returned 1 exit status 原因是系统无法找到pthread_create函数.也就是说编译器在link得时候找不到其中的一个使用库的函数. 解决办法如下: For Linux the correct command is: gcc -pthread xxx.c In general, libraries should follow s