2015-11-13 346 views
0

我正試圖學習c中的多線程程序,並得到了一個鏈接器錯誤,我無法解決我已經檢查了類似於此的上一個問題,但這些解決方案可能沒有解決我的問題。c線程程序中的鏈接器錯誤.. ld返回1退出狀態

error : 
single_thread.c:(.text+0x15)undefined reference to 'thread_function' 
collect2:ld returned 1 exit status 

我已經檢查了錯字的

的程序是這樣的

 #include<stdio.h> 
     #include<unistd.h> 
     #include<pthread.h> 
     #include<stdlib.h> 
     #include<string.h> 

    void *thread_functions(void *arg); 

    char message[]="Hello world"; 

    int main() 
    { 

     int res; 
     pthread_t a_thread; 
     void *thread_res; 

     res=pthread_create(&a_thread,NULL,thread_functions,(void*)message); 
//i guess error is from the function pointer. 
     if(res!=0) 
     { 
      perror("thread creation:"); 
      exit(EXIT_FAILURE); 
     } 

     printf("waiting for thread to finish...\n"); 
     pthread_join(a_thread,NULL); 
    /* if(res!=0) 
     { 
      perror("thread_join failed:"); 
      exit(EXIT_FAILURE); 
     }*/ 
    // printf("thread joined,it has returned %s\n",(char*)thread_res); 
     printf("Message:%s\n",message); 
     exit(EXIT_SUCCESS); 
    } 

    void *thread_fucntions(void *arg) 
    { 
     printf("thread function is running.argument was %s\n",(char*)arg); 
     sleep(3); 
     strcpy(message,"BYE!"); 
     pthread_exit("thank you for the cpu time"); 
    } 
+1

'我已經檢查了錯字的'...不知何故,我對此表示懷疑。 :) –

+0

我有。用-lpthread編譯代碼。請幫助我提出問題的格式。我沒有正確地得到它 – user3207191

+0

在這段代碼中似乎有一個類型,我想這是「void * thread_fucntions(void * arg)」這個問題在底部拼寫錯誤。 – cedd

回答

0

您需要編譯這樣

gcc single.thread.c -lpthread 

錯字

void *thread_fucntions(void *arg) 
       ^^ 
+1

我不認爲這是這個問題,:) –

+0

@SouravGhosh,我明白了。 :) – Haris

+0

我這樣做了..請也幫我與格式問問題。我沒有得到它正確的 – user3207191

1

您需要命名正好與前向聲明和定義時間相同的。您的編譯器會看到函數thread_functions()的前向聲明以及對它的調用,但在鏈接期間,鏈接程序不會看到相同的定義,因爲您在那裏有拼寫錯誤。所以它尖叫。

變化

void *thread_fucntions(void *arg) 

void *thread_functions(void *arg) 
+0

比你先生我明白了..不能正確地看到 – user3207191

+0

@SergeyA點!讓我編輯來糾正它。 –

相關問題