How to link librt (lrt) When timer_create is used

Source: Internet
Author: User

Because POSIX timer is used, librt (real-time dynamic library of glibc) must be dynamically linked during compilation ).

The-lrt parameter must be added to the makefile link:

Gcc-LrtA. o, B. o...-o my_program

This makefile runs well on the local machine. The system is Ubuntu 11.04 Desktop.

However, the following error occurs on the server:

My_timer.o: In function 'init _ my_timer ':
My_timer.c :(. text + 0x1bb): undefined reference to 'timer _ create'
My_timer.o: In function 'cel _ 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

Obviously, librt is not linked! Very strange, the server is a Linux ubuntu-wfms-slaver-02 3.0.0-15-server

However, you can use ldconfig-p to view all the dynamic libraries and see that there is a 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

I am very puzzled by this question!

Accidentally put the-lrt parameter at the end:

Gcc a. o, B. o...-o my_program-Lrt

The result is successful ~ I don't quite understand why the parameter location has an impact.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.