[問題] 關於pthread.h

看板Linux作者 (人生遊戲 戲遊人生)時間16年前 (2009/11/22 15:37), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
我寫了一個程式 欲印出下列訊息: This is Hello thread. Thread ID: Argument:100 ------------------------------------------------ 程式碼如下 ------------------------------------------------ #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *PrintHello(void *arg) { printf("This is Hello thread.\n"); printf("Thread ID: %lu\n",pthread_self()); printf("Argument: %d\n",(int) arg); pthread_exit(NULL); } int main() { pthread_t thread; int rc,t = 100; rc = pthread_create(&thread,NULL,PrintHello,(void *)t); if(rc){ printf("ERROR; return the code from the pthread_creat() is %d\n",rc); exit(-1); } rc = pthread_join(thread,NULL); if(rc){ printf("ERROR; return the code from the pthread_join() is %d\n",rc); exit(-1); } return 0; } ------------------------(分隔線)---------------------------- 結果gcc編譯時出現以下訊息: /tmp/cco6vngs.o: In function `main': thread_example.c:(.text+0x79): undefined reference to `pthread_create' thread_example.c:(.text+0xb9): undefined reference to `pthread_join' collect2: ld returned 1 exit status 這是哪裡出問題 小弟不太懂 有那位大大能回答我的 感激不盡 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.240.157.185 amu1661024:轉錄至看板 LinuxDev 11/22 15:43

11/22 15:51, , 1F
需要link librery,compile時加上-lpthread or -pthread
11/22 15:51, 1F

11/22 15:52, , 2F
a
11/22 15:52, 2F

11/22 15:55, , 3F
真的可以了耶 謝謝^^
11/22 15:55, 3F
文章代碼(AID): #1B2Ef55B (Linux)