亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Linux中未定義的對p線程_CREATE的引用

Linux中未定義的對p線程_CREATE的引用

白衣非少年 2019-07-04 12:56:37
Linux中未定義的對p線程_CREATE的引用我從網上獲得了下面的演示https:/Computing.llnl.gov/tutorials/p線程/#include <pthread.h>#include <stdio.h>#define NUM_THREADS     5void *PrintHello(void *threadid){    long tid;    tid = (long)threadid;    printf("Hello World! It's me, thread #%ld!\n", tid);    pthread_exit(NULL);}int main (int argc, char *argv[]){    pthread_t threads[NUM_THREADS];    int rc;    long t;    for(t=0; t<NUM_THREADS; t++){       printf("In main: creating thread %ld\n", t);       rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);       if (rc){          printf("ERROR; return code from pthread_create() is %d\n", rc);          exit(-1);       }    }    pthread_exit(NULL);}但是,當我在我的機器上編譯它(運行UbuntuLinux 9.04)時,我得到以下錯誤:corey@ubuntu:~/demo$ gcc -o term term.c term.c: In function ‘main’:term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’/tmp/cc8BMzwx.o:  In function `main':term.c:(.text+0x82): undefined reference to `pthread_create'collect2: ld returned 1 exit status這對我來說沒有任何意義,因為標題包括pthread.h,它應該有pthread_create功能。有什么問題嗎?
查看完整描述

3 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

到目前為止,這兩個問題的答案都是不正確.
對于Linux,正確的命令是:

gcc -pthread -o term term.c

一般來說,庫應該遵循命令行中的源和對象,并且-lpthread不是“選項”,而是一個庫規范。在一個只有libpthread.a安裝,

gcc -lpthread ...

將無法連接。


查看完整回答
反對 回復 2019-07-04
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

屬性->c/c+構建->設置->GCC C+鏈接器->頂部添加“p線程”庫


查看完整回答
反對 回復 2019-07-04
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

從Linux終端運行,對我起作用的是使用以下命令進行編譯(假設我要編譯的c文件稱為test.c):

gcc -o test test.c -pthread

希望它能幫到別人!


查看完整回答
反對 回復 2019-07-04
  • 3 回答
  • 0 關注
  • 766 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號