linux - 对另一个库中函数的 undefined reference

标签 linux gcc

我正在尝试使用对一个库的引用来编译目标代码。这是 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_createtimer_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/

相关文章:

linux - Linux C++ IDE 中的解决方案文件相当于什么

linux - Komodo IDE FTP(ASCII,二进制)行尾字符

c++ - 如何设置 eclipse 以使用 arm 的代码源工具链

c - 通过指针表示法写入二维数组

cast 增加了目标类型与指针的对齐要求

linux - 如何调试本地主机 - 无法访问该站点

java - 如何在 Java 中向 USB 行式打印机发送命令

python - 如何在python中以相反的顺序打印字符串数组

c++ - 为什么在 Mac OS X 上使用 size_t 时 uint32_t 和 uint64_t 之间存在歧义?

gcc - GCC 在 VPS 上分配内存的问题