因爲要用到POSIX的定時器,編譯時要動態鏈接librt(glibc的實時動態庫)。
makefile鏈接時需要添加-lrt參數:
gcc -lrt a.o,b.o............. -o my_program
這個makefile在本地機器上運行得好好的,系統是Ubuntu 11.04 Desktop。
但是在服務器上就出現如下錯誤:
my_timer.o: In function `init_my_timer':
my_timer.c:(.text+0x1bb): undefined reference to `timer_create'
my_timer.o: In function `cancel_my_timer':
my_timer.c:(.text+0x220): undefined reference to `timer_settime'
my_timer.o: In function `start_my_timer':
my_timer.c:(.text+0x255): undefined reference to `timer_settime'
my_timer.o: In function `restart_my_timer':
my_timer.c:(.text+0x28a): undefined reference to `timer_settime'
collect2: ld returned 1 exit status
make: *** [my_program] Error 1
很明顯是librt沒有鏈接上!非常奇怪,服務器是 Linux ubuntu-wfms-slaver-02 3.0.0-15-server
但是用 ldconfig -p 查看所有的動態庫,看見是有librt的:
librt.so.1 (libc6,x86-64, OS ABI: Linux 2.6.15) => /lib/x86_64-linux-gnu/librt.so.1
librt.so (libc6,x86-64, OS ABI: Linux 2.6.15) => /usr/lib/x86_64-linux-gnu/librt.so
對這個問題非常不解!
偶然嘗試將 -lrt 參數放在最後面:
gcc a.o,b.o............. -o my_program -lrt
結果就成功了~ 不太明白爲什麼參數的位置會有影響。