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選項后面,居然就通過了,也算是比較烏龍吧。