解决APUE编译错误

今天试着敲了一下APUE的小例子,遇到了个错误 -----  undefined reference to `pthread_create。(为自己这么晚接触多线程惭愧)。

上网上查了一下,借人经验。

问题原因:
    pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。

问题解决:
    在编译中要加 -lpthread参数
    gcc 6.c -o 6 -lpthread
    6.c为你些的源文件,不要忘了加上头文件#include<pthread.h>

 

-------------------------------------------------------------------------------------------------------

 

undefined reference to `clock_gettime

下面这个错误通常是因为链接选项里漏了-lrt,但有时发现即使加了-lrt仍出现这个问题,使用nm命令一直,会发现-lrt最终指向的文件没有包含任何symbol,这个时候,可以找相应的静态库版本librt.a,看看它里面是否存在`clock_gettime'。
 
/data1/mooon/run/lib/libsys.a(lock.o): In function `sys::CLock::timed_lock(unsigned int)':
/data1/mooon/src/common_library/src/sys/./lock.cpp:101: undefined reference to `clock_gettime'
/data1/mooon/run/lib/libsys.a(event.o): In function `sys::CEvent::timed_wait(sys::CLock&, unsigned int)':
/data1/mooon/src/common_library/src/sys/./event.cpp:56: undefined reference to `clock_gettime'
 
提示:使用/usr/lib/x86_64-linux-gnu/librt.a替代-lrt,32位系统路径可能是/usr/lib/i386-linux-gnu/librt.a,注意/usr/lib/i386-linux-gnu/librt.a可能需要放在链接选项的最后,不在可能依然,因为静态库间是有依赖关系的。
 
---------------------------------------------------------------------------------
 
undefined reference to 'heapsort' 
 

  • barrier.c:(.text+0x80): undefined reference to `heapsort'
出现该错误是因为heapsort在linux下没有相关的库,在网上搜索发现这个函数和libbsd-devel相关,按照下面的命令安装即可,安装完成后,在使用gcc编译的时候,加上-lbsd编译选项即可:
yum install libbsd-devel
posted @ 2023-08-29 16:38  HelloMarsMan  阅读(60)  评论(0)    收藏  举报