c线程程序中的链接器错误.. ld返回1退出状态

时间:2015-11-13 17:10:26

标签: c multithreading linker-errors

我试图在c中学习多线程程序并得到一个我无法解决的链接器错误我已经检查了类似于此的上一个问题,但这些解决方案无法解决我的问题。

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");
    }

2 个答案:

答案 0 :(得分:1)

您需要将函数命名为,与前向声明和定义时间相同。您的编译器会看到函数thread_functions()的前向声明及其调用,但在链接时,链接器无法看到相同的定义,因为您在那里有拼写错误。所以它尖叫。

更改

void *thread_fucntions(void *arg)

void *thread_functions(void *arg)

答案 1 :(得分:0)

你需要像这样编译

<a name="jump"></a>

错字

gcc single.thread.c -lpthread