简体   繁体   中英

linker error in c thread program.. ld returned 1 exit status

i was trying to learn multithreading program in c and got a linker error which I am not able to solve I have checked the previous problem similar to this but those solutions could not solve my problem.

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

I have checked the typo's

the program goes like this

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

You need to name the functions exactly the same as forward declaration and definition time. Your compiler sees the forward declaration of the function thread_functions() and the call to it, but during the linking time, linker does not get to see a definition of the same, as you're having a typo there. So it screams.

Change

void *thread_fucntions(void *arg)

to

void *thread_functions(void *arg)

You need to compile like this

gcc single.thread.c -lpthread

Typo

void *thread_fucntions(void *arg)
               ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM