国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Linux下undefined reference to ‘pthread_create’問題解決

2019-11-10 21:30:58
字體:
供稿:網(wǎng)友

接觸了linux系統(tǒng)編程中的線程編程模塊,可gcc sample.c(習(xí)慣把書上的sample代碼寫進(jìn)sample.c文件中)出現(xiàn)“undefined reference to ‘pthread_create’”,所有關(guān)于線程的函數(shù)都會(huì)有此錯(cuò)誤,導(dǎo)致無法編譯通過。

問題的原因:pthread不是Linux下的默認(rèn)的庫,也就是在鏈接的時(shí)候,無法找到phread庫中哥函數(shù)的入口地址,于是鏈接會(huì)失敗。

解決:在gcc編譯的時(shí)候,附加要加 -lpthread參數(shù)即可解決。

#include <stdio.h> #include <pthread.h> #include <unistd.h> pthread_t ntid; void PRintids(const char * s) {     pid_t pid;     pthread_t tid;     pid = getpid();     tid = pthread_self();     printf("%s pid %u tid %u (0x%x)/n",s,(unsigned int)pid,             (unsigned int)tid,(unsigned int)tid); } void * thr_fn(void * arg) {     printids("new thread:");     return ((void *)0); } int main(void) {     int err;     err = pthread_create(&ntid,NULL,thr_fn,NULL);     if(err != 0)         printf("pthread_create error /n");     printids("main thread:");     sleep(1);     return 0; }

root@daoluan:/code/pthreadid# gcc sample.c/tmp/cc1WztL9.o: In function `main’:sample.c:(.text+0×83): undefined reference to `pthread_create’collect2: ld returned 1 exit status

root@daoluan:/code/pthreadid# gcc -lpthread sample.croot@daoluan:/code/pthreadid# ./a.outmain thread: pid 7059 tid 3078141632 (0xb778b6c0)new thread: pid 7059 tid 3078138736 (0xb778ab70)

本文完 2012-07-15

搗亂小子 http://www.daoluan.net/

 

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

轉(zhuǎn)自:http://blog.csdn.net/jiangxinyu/article/details/7778864


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳春市| 包头市| 罗定市| 朔州市| 青浦区| 灯塔市| 崇礼县| 定边县| 忻城县| 靖远县| 蒙自县| 新化县| 资阳市| 山东| 乌拉特后旗| 南乐县| 永泰县| 内黄县| 固始县| 宾阳县| 永寿县| 弥渡县| 翼城县| 浦城县| 南召县| 盐山县| 白城市| 临汾市| 大连市| 石林| 祁门县| 信阳市| 安国市| 敖汉旗| 宁都县| 嫩江县| 扎赉特旗| 宝坻区| 宣威市| 梨树县| 济南市|