Undefined reference to a function in another library

前端 未结 4 1393
自闭症患者
自闭症患者 2021-01-25 04:24

I\'m trying to compile an object code with a reference to one lib. This is the code of libexample.c:

#include \"libexample.h\"
#include 
#include         


        
相关标签:
4条回答
  • 2021-01-25 04:26

    The man timer_create command explains you:

    NAME
       timer_create - create a POSIX per-process timer
    
    SYNOPSIS
       #include <signal.h>
       #include <time.h>
    
       int timer_create(clockid_t clockid, struct sigevent *sevp,
                        timer_t *timerid);
    
       Link with -lrt.
    

    So you should, as documentation says, link with -lrt.

    So use

     gcc libexample.c -fPIC -shared -o libexample.so -lrt
    

    to produce your libexample.so.

    As undur_gongor commented, you need to put the libraries in good order after all the rest (the usual order for gcc arguments is source files, object files, libraries in dependency order) in gcc or ld commands (and that is documented in ld documentation, and in gcc ones). So -lrt should go last.

    And learn to read man pages.

    0 讨论(0)
  • 2021-01-25 04:34

    If you are using cmake, make sure you include the libraries using target_link_libraries(). For e.g., timer functions like timer_create() you need "rt" and for pthread you need "pthread" added using target_link_libraries().

    0 讨论(0)
  • 2021-01-25 04:37

    Looks like you forgot to link in the library that defines timer_create and timer_settime -- you need to add -lrt to your gcc command.

    (source: http://www.kernel.org/doc/man-pages/online/pages/man2/timer_create.2.html)

    0 讨论(0)
  • 2021-01-25 04:50

    Add -lrt to your link command. timer_create and timer_settime are not part of the C Standard library.

    gcc -fPIC -shared libexample.c -lrt -o libexample.so
    gcc -L. example.c -lexample -o example
    
    0 讨论(0)
提交回复
热议问题