照著GUN/Linux編程指南中的一個(gè)例子輸入編譯,結(jié)果出現(xiàn)如下錯(cuò)誤:
undefined reference to 'pthread_create'
undefined reference to 'pthread_join'
問題原因:
pthread 庫(kù)不是 Linux 系統(tǒng)默認(rèn)的庫(kù),連接時(shí)需要使用靜態(tài)庫(kù) libpthread.a,所以在使用pthread_create()創(chuàng)建線程,以及調(diào)用 pthread_atfork()函數(shù)建立fork處理程序時(shí),需要鏈接該庫(kù)。
問題解決:
在編譯中要加 -lpthread參數(shù)
gcc thread.c -o thread -lpthread
thread.c為你些的源文件,不要忘了加上頭文件#include<pthread.h>