Find the answer to your Linux question:
Results 1 to 8 of 8
Here is the code: Code: #include <stdio.h> #include <pthread.h> void *thread(void *vargp) { printf("Hello World!\n"); return NULL; } int main() { pthread_t tid; pthread_create(&tid , NULL , thread ,NULL); pthread_join(tid ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Location
    Liuyang,Hunan,China
    Posts
    4

    Unhappy A Link Error about Pthreads

    Here is the code:
    Code:
    #include <stdio.h>
    #include <pthread.h>
    
    void *thread(void *vargp)
    {
    	printf("Hello World!\n");
    	return NULL;
    }
    
    int main()
    {
    	pthread_t tid;
    	pthread_create(&tid , NULL , thread ,NULL);
    	pthread_join(tid , NULL);
    	return 0;
    }
    When I type "gcc m.c" in the shell,it says
    /home/remir/tmp/ccOoOzic.o: In function `main':
    m.c.text+0x39): undefined reference to `pthread_create'
    m.c.text+0x47): undefined reference to `pthread_join'
    collect2: ld returned 1 exit status

    How to solve this problem?Thanks!

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    You have to link it with -lpthread. Including the headers and linking are not the same.

    gcc -o whatever whatever.c -lpthread -Wall

    I usually do -Wall cause warnings should be on It's a good habit to get into making warning-free code.

  3. #3
    Just Joined!
    Join Date
    Jul 2007
    Location
    Liuyang,Hunan,China
    Posts
    4
    Thanks for your advise.

    What if I use the routines in <semaphore.h>?

    I'm new guy.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    To use functions as sem_init, sem_wait, sem_trywait you must include <semaphore.h>.

    Regards

  5. #5
    Just Joined!
    Join Date
    Jul 2007
    Location
    Liuyang,Hunan,China
    Posts
    4
    But it says
    undefined reference to 'sem_init'.

    I think I may link it with some options,but I don't know.

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Can you copy your code and paste it here within code blocks?

    Regards

  7. #7
    Just Joined!
    Join Date
    Jul 2007
    Location
    Liuyang,Hunan,China
    Posts
    4
    Code:
    #include <semaphore.h>
    
    int main()
    {
    	sem_t m;
    	sem_init(&m , 0 , 1);
    	return 0;
    }
    It's simple code.
    But it has a link error:
    undefined reference to 'sem_init'.

    What does mean of "Regards"?Why your two posts in this thread both have this word?
    I'm not English speaker,Thanks!

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    It's simple code.
    But it has a link error:
    undefined reference to 'sem_init'.
    Use the -lpthread link option.

    What does mean of "Regards"?Why your two posts in this thread both have this word?
    I'm not Enlish speaker,Thanks!
    I'm also not .
    Regards is a synonym for greetings.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...