对pthread_create未定义的引用
Published:
本篇博客记录编译CSAPP课本code/ecf/waitpid1.c的错误调试过程
在Linux编译,出现错误:
/tmp/ccYURWPy.o: In function `main':
waitpid1.c:(.text+0x12): undefined reference to `Fork'
waitpid1.c:(.text+0xb6): undefined reference to `unix_error'
collect2: error: ld returned 1 exit status
(base) [root@instance-3kolm650 ecf]# gcc -I. -o waitpid1 waitpid1.c csapp.c
/tmp/ccnEDu7P.o: In function `Pthread_create':
csapp.c:(.text+0x1049): undefined reference to `pthread_create'
/tmp/ccnEDu7P.o: In function `Pthread_cancel':
csapp.c:(.text+0x107b): undefined reference to `pthread_cancel'
/tmp/ccnEDu7P.o: In function `Pthread_join':
csapp.c:(.text+0x10b8): undefined reference to `pthread_join'
/tmp/ccnEDu7P.o: In function `Pthread_detach':
csapp.c:(.text+0x10ea): undefined reference to `pthread_detach'
/tmp/ccnEDu7P.o: In function `Pthread_once':
csapp.c:(.text+0x114a): undefined reference to `pthread_once'
/tmp/ccnEDu7P.o: In function `Sem_init':
csapp.c:(.text+0x1172): undefined reference to `sem_init'
/tmp/ccnEDu7P.o: In function `P':
csapp.c:(.text+0x119a): undefined reference to `sem_wait'
/tmp/ccnEDu7P.o: In function `V':
csapp.c:(.text+0x11c2): undefined reference to `sem_post'
collect2: error: ld returned 1 exit status
出现原因,由于csapp.h引用了pthread库,不是linux系统默认库,链接时候需要使用库libpthread.a,所以在使用pthread_create创建线程时候,要在编译中加-lpthread参数选项
gcc -I. -lpthread -o xxx
-I. 表示在本目录寻找头文件