使用tcmalloc编译出现undefined reference to `sem_init'

tcmalloc是Google开源的一个内存管理库, 作为glibc malloc的替代品,效率大概是gclibc malloc的几倍。想在工程中用上tcmalloc非常的简单,我们采用了静态编译的方式,通过增加链接选项-ltcmalloc静态链接libtcmalloc.a即可。但是在链接过程中出现了意外情况,报出了如下错误:

../3party/static_libs/libtcmalloc.a(libtcmalloc_la-linuxthreads.o): In function `TCMalloc_ListAllProcessThreads':
linuxthreads.cc:(.text+0x333): undefined reference to `sem_init
' linuxthreads.cc:(.text+0x41c): undefined reference to `sem_post'
linuxthreads.cc:(.text+0x477): undefined reference to `sem_destroy'
../3party/static_libs/libtcmalloc.a(libtcmalloc_la-linuxthreads.o): In function `ListerThread':
linuxthreads.cc:(.text+0x67b): undefined reference to `sem_wait
' collect2: error: ld returned 1 exit status

sem_init是库函数,头文件是#include<semaphore.h>。搜索了很多资料,都说是缺少了链接选项-lpthread,但是我们的Makefile是有-lpthread选项的,又尝试了在编译选项里面加-pthread,但还是没有效果,各种办法都尝试了之后,最后试着把-lpthread选项放到了-tcmalloc选项后面,居然就通过了,也算是比较乌龙吧。

原文地址:https://www.cnblogs.com/minglee/p/10096457.html