gcc:对函数的未定义引用



我知道有很多类似的问题,但我别无选择,只能发布这个问题,因为我没有找到我的问题的答案,即使经过几个小时的谷歌搜索。我希望你能理解。

我与gcc链接如下。

gcc myprogram.c csapp.c -o a.out

csapp.c定义了myprogram.c中使用的函数。Myprogram.c包含了csapp.h.

出现以下错误:

/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_create':
csapp.c:(.text+0x121e): undefined reference to `pthread_create'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_cancel':
csapp.c:(.text+0x1257): undefined reference to `pthread_cancel'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_join':
csapp.c:(.text+0x129b): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_detach':
csapp.c:(.text+0x12d4): undefined reference to `pthread_detach'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Pthread_once':
csapp.c:(.text+0x1343): undefined reference to `pthread_once'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `Sem_init':
csapp.c:(.text+0x1370): undefined reference to `sem_init'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `P':
csapp.c:(.text+0x139f): undefined reference to `sem_wait'
/usr/bin/ld: /tmp/ccIWxAk3.o: in function `V':
csapp.c:(.text+0x13ce): undefined reference to `sem_post'
collect2: error: ld returned 1 exit status

这些是在capp .c中明确定义的。为什么会出现这些错误?

您应该编译并链接-pthread

最新更新