linux下编译多线程程序, undefined reference to `pthread_create',undefined reference to `pthread_join'

时间:2021-10-12 05:30:12

问题描述:hero@ubuntu:~/WORK/for_xc7z035/app/sdr_7010$ gcc -o ./test ./test.c
/tmp/cc7BcXdf.o: In function `main':
test.c:(.text+0x1d7): undefined reference to `pthread_create'
test.c:(.text+0x1fb): undefined reference to `pthread_create'
test.c:(.text+0x21f): undefined reference to `pthread_create'
test.c:(.text+0x233): undefined reference to `pthread_join'
test.c:(.text+0x247): undefined reference to `pthread_join'
test.c:(.text+0x267): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

问题原因:

  1, pthread 库不是 Linux 系统默认的库,在编译多线程程序时,需要在编译命令中添加链接libpthread.a库。

 2,忘了加上头文件#include<pthread.h>


问题解决:
    在编译中要加 -lpthread参数
    gcc  ./test.c -o ./test -lpthread