Undefined reference to a function in another library

前端 未结 4 1416
自闭症患者
自闭症患者 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 
       #include 
    
       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.

提交回复
热议问题