日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pthread_create函数编译时报错:undefined reference to 'pthread_create'

發(fā)布時間:2025/3/20 编程问答 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pthread_create函数编译时报错:undefined reference to 'pthread_create' 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

錯誤:

pthread_create函數(shù)編譯時報錯:undefined reference to 'pthread_create'

pthread_create()和pthread_atfork()函數(shù)使用時應注意的問題:

源代碼:

#include <pthread.h>

void pmsg(void* p)
{
??? char *msg;
??? msg = (char*)p;
??? printf("%s ", msg);
}

int main(int argc, char *argv)
{
??? pthread_t t1, t2;
??? pthread_attr_t a1, a2;
??? char *msg1 = "Hello";
??? char *msg2 = "World";

??? pthread_attr_init(&a1);
??? pthread_attr_init(&a2);
??? pthread_create(&t1, &a1, (void*)&pmsg, (void*)msg1);
??? pthread_create(&t2, &a2, (void*)&pmsg, (void*)msg2);

??? return 0;
}
運行結果:
gcc thread.c -o thread
/tmp/ccFCkO8u.o: In function `main':
/tmp/ccFCkO8u.o(.text+0x6a): undefined reference to `pthread_create'
/tmp/ccFCkO8u.o(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

原因分析:

由于pthread 庫不是?Linux?系統(tǒng)默認的庫,連接時需要使用靜態(tài)庫 libpthread.a,所以在使用pthread_create()創(chuàng)建線程,以及調(diào)用 pthread_atfork()函數(shù)建立fork處理程序時,在編譯中要加 -lpthread參數(shù)。

解決辦法:

例如:在加了頭文件#include <pthread.h>之后執(zhí)行 pthread.c文件,需要使用如下命令:

??? gcc thread.c -o thread?-lpthread

這種情況類似于<math.h>的使用,需在編譯時加 -m 參數(shù)。

總結

以上是生活随笔為你收集整理的pthread_create函数编译时报错:undefined reference to 'pthread_create'的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。