看板 Linux 關於我們 聯絡資訊
我寫了一個程式 欲印出下列訊息: 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
ttnwosay:需要link librery,compile時加上-lpthread or -pthread 11/22 15:51
ttnwosay: a 11/22 15:52
amu1661024:真的可以了耶 謝謝^^ 11/22 15:55