我正在尝试使用对一个库的引用来编译目标代码。这是 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);
}
以及示例程序的简单代码:
#include "libexample.h"
int main() {
init();
return 0;
}
我用它来编译:
gcc libexample.c -c -lrt -o libexample.o
gcc example.c -lrt ibexample.o -o example
当我尝试使用第二行进行编译时,我得到了这个:
./libexample.so: undefined reference to `timer_create'
./libexample.so: undefined reference to `timer_settime'
有人知道我做错了什么吗?
最佳答案
将 -lrt
添加到您的链接命令。 timer_create
和 timer_settime
不是 C 标准库的一部分。
gcc -fPIC -shared libexample.c -lrt -o libexample.so
gcc -L. example.c -lexample -o example
关于linux - 对另一个库中函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9010833/