2015-11-13 341 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点!让我编辑来纠正它。 –

相关问题