C 当POSIX函数';再进口

C 当POSIX函数';再进口,c,time,timer,posix,real-time,C,Time,Timer,Posix,Real Time,我只是用计时器创建一个线程。即使我导入了所有需要的库。它一直告诉我们没有函数的引用 确切地说,timer\u create(…)和timer\u settime(…)是未引用的 以下是我的作品: #include <stdio.h> #include <stddef.h> #include <sched.h> #include <sys/mman.h> #include <sys/time.h> #include <signal.

我只是用计时器创建一个线程。即使我导入了所有需要的库。它一直告诉我们没有函数的引用

确切地说,
timer\u create(…)
timer\u settime(…)
是未引用的

以下是我的作品:

#include <stdio.h>
#include <stddef.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

我希望能得到一些帮助。谢谢

构建程序分为两个阶段

  • 汇编
  • 联动
在编译阶段,您需要知道构建
.o
文件的函数和结构的声明

链接
阶段,您需要实际实现这些功能

未定义引用错误告诉您链接器找不到函数的实现

因为您使用第三方库,所以需要使用
-l
键链接该库

要检查库是否有符号,可以执行

$ nm -gD /usr/lib/x86_64-linux-gnu/librt.so | grep timer_create
0000000000004520 T timer_create
00000000000042a0 T timer_create

在您的情况下,您需要使用
-lrt
键链接
rt
库。

您确定链接了正确的库吗?请为代码段发布生成命令好吗?如果是在Linux上,请注意这些函数的手册页上写着“link with-lrt”。“undefined reference”(标题中提到)与文本中提到的“unreferenced”不同。是哪一个?“undefined reference”是链接器错误而不是编译器错误-未能包含适当的标头将导致“undeclared reference”。包含“导入”声明,而不是定义。@EricPostphil认为可能我不会说英语,我的Linux没有用英语向我显示错误。我完全知道如何复制和粘贴,但这只是一个翻译问题。放松。@wasanga7对于英文错误消息,请尝试
LC_ALL=C LANG=C gcc blabla
非常感谢。只需将
-lrt
添加到命令中,它就解决了我的问题。很高兴为您提供帮助,祝您在编程方面取得更大成就。
gcc -o ejer1 ejer1.c -lpthread
$ nm -gD /usr/lib/x86_64-linux-gnu/librt.so | grep timer_create
0000000000004520 T timer_create
00000000000042a0 T timer_create