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 <signal.h>
#include <time.h>
timer_t sched;
struct itimerspec timer = {{0, 0}, {0, 0}};
void init() {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &sched);
timer_settime(sched, TIMER_ABSTIME, &timer, NULL);
}
And the simple code of a example program:
#include "libexample.h"
int main() {
init();
return 0;
}
I use this to compile:
gcc libexample.c -c -lrt -o libexample.o
gcc example.c -lrt ibexample.o -o example
And I get this when I’m trying to compile with the second line:
./libexample.so: undefined reference to `timer_create'
./libexample.so: undefined reference to `timer_settime'
Anyone knows what I’m doing wrong?
The
man timer_createcommand explains you:So you should, as documentation says, link with
-lrt.So use
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
gccarguments is source files, object files, libraries in dependency order) ingccorldcommands (and that is documented inlddocumentation, and ingccones). So-lrtshould go last.And learn to read man pages.