使用timer_create時連結librt(lrt)的問題

來源:互聯網
上載者:User

因為要用到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

結果就成功了~ 不太明白為什麼參數的位置會有影響。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.