编译Linux多线程最简单的一个例子时,编译时出现如下错误
undefined reference to 'pthread_create'
undefined reference to 'pthread_join'
用man命令查看pthread_create和pthread_join方法时,确实只用包括头文件pthread.h即可
原因:
pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_join()等待线程时,需要链接该库。
问题解决:
在编译中要加 -lpthread参数
gcc test -o thread_test.c -lpthread