Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I'm working on an application of C programming language. I want to build a code that uses multitasking; at the moment I'm just experimenting with examples I got from ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Location
    Obera, Misiones, Argentina
    Posts
    3

    Help with pthread

    Hi, I'm working on an application of C programming language. I want to build a code that uses multitasking; at the moment I'm just experimenting with examples I got from books and WEBside. Many of them use pthread_create () to create a thread. I don't know why, but my compiler prints an error message that says: ".../home/investigacion/ejpthread/src/ejpthread.c:62: undefined reference to `pthread_create'". First I though it was because I didn't include the respective header (pthread.h), but even after I wrote a code line including it, the message was printed again every time I compiled my code.
    Inspired in an example I found in a book of Linux programming, I wrote following code:
    #include <pthread.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>


    void *child_fn (void *arg);

    int main (void)
    {

    pthread_t tchild;
    int algo;
    algo = pthread_create (&tchild, NULL, &child_fn, NULL );
    if (algo != 0)
    perror ("Pthreads error");

    pthread_join (tchild, NULL);
    return 0;
    }

    void *child_fn (void *arg)
    {

    puts ("Soy un thread hijo"); //This is spanish "I'm a thread child"
    return NULL;
    }

    Similarly occurs with function pthread_join(); compiler returns ERROR.
    I'll appreciate your help. Thanks!

  2. #2
    Just Joined!
    Join Date
    Aug 2009
    Posts
    3
    Quote Originally Posted by curi View Post
    Hi, I'm working on an application of C programming language. I want to build a code that uses multitasking; at the moment I'm just experimenting with examples I got from books and WEBside. Many of them use pthread_create () to create a thread. I don't know why, but my compiler prints an error message that says: ".../home/investigacion/ejpthread/src/ejpthread.c:62: undefined reference to `pthread_create'". First I though it was because I didn't include the respective header (pthread.h), but even after I wrote a code line including it, the message was printed again every time I compiled my code.
    Inspired in an example I found in a book of Linux programming, I wrote following code:
    #include <pthread.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>


    void *child_fn (void *arg);

    int main (void)
    {

    pthread_t tchild;
    int algo;
    algo = pthread_create (&tchild, NULL, &child_fn, NULL );
    if (algo != 0)
    perror ("Pthreads error");

    pthread_join (tchild, NULL);
    return 0;
    }

    void *child_fn (void *arg)
    {

    puts ("Soy un thread hijo"); //This is spanish "I'm a thread child"
    return NULL;
    }

    Similarly occurs with function pthread_join(); compiler returns ERROR.
    I'll appreciate your help. Thanks!
    Is it a compiler or linker error. Compiling should have no problem if you have the header files but you will have to add "-lpthread" or something similar to you linking options. For example: gcc -o foo foo.c -lpthread

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Location
    Obera, Misiones, Argentina
    Posts
    3

    ˇˇGenio!!

    ˇMuchisimas gracias! A lot of thanks!!!!

    That's what I needed; I have alredy tried, and it worked!

Posting Permissions

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