pthread error

Discussion in 'C Programming' started by asit, Aug 11, 2009.

  1. asit

    asit Guest

    This is the code snippet with main function..

    #include <stdio.h>
    #include <pthread.h>

    pthread_t ntid;

    void printids(const char *s) {
    pid_t pid;
    pthread_t tid;
    pid = getpid();
    tid = pthread_self();
    printf("%s pid %u tid %u (0x%x) \n", s, (unsigned int)pid, (unsigned
    int)tid, (unsigned int)tid);
    }

    void * thr_fn(void *arg) {
    printids("new thread : ");
    return ((void*)0);
    }

    int main() {
    int err;
    err = pthread_create(&ntid, NULL, thr_fn, NULL);
    if(err != 0)
    printf("can't create thread \n");
    printids("main thread : ");
    sleep(1);
    return 0;
    }

    When I tried to compile this, it showed the following error ???

    asit@asit-desktop:~/cpp$ gedit pthread1.c
    asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c
    /tmp/ccgO2hyj.o: In function `main':
    pthread1.c:(.text+0x85): undefined reference to `pthread_create'
    collect2: ld returned 1 exit status
     
    asit, Aug 11, 2009
    #1
    1. Advertising

  2. asit

    Hamiral Guest

    asit a écrit :
    > asit@asit-desktop:~/cpp$ gedit pthread1.c
    > asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c
    > /tmp/ccgO2hyj.o: In function `main':
    > pthread1.c:(.text+0x85): undefined reference to `pthread_create'
    > collect2: ld returned 1 exit status


    You forgot to link with pthread...

    Try compiling with
    gcc -o pthread1 pthread1.c -lpthread
     
    Hamiral, Aug 11, 2009
    #2
    1. Advertising

  3. (Off-topic for comp.lang.c, moving to comp.unix.programmer where
    this seems more suited.)

    On Tue, 11 Aug 2009 17:38:26 +0200, Hamiral <> wrote:
    >>> asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c

    >> /tmp/ccgO2hyj.o: In function `main':
    >> pthread1.c:(.text+0x85): undefined reference to `pthread_create'
    >> collect2: ld returned 1 exit status

    >
    > You forgot to link with pthread...
    >
    > Try compiling with
    > gcc -o pthread1 pthread1.c -lpthread


    With gcc, you should rather use -pthread which should set both preprocessor
    and linker flags appropriately for using pthreads on the current platform.

    Just -lpthread may of course work for some use cases.

    --
    Mikko Rauhala <> - http://www.iki.fi/mjr/blog/
    The Finnish Pirate Party - http://piraattipuolue.fi/
    World Transhumanist Association - http://transhumanism.org/
    Singularity Institute - http://singinst.org/
     
    Mikko Rauhala, Aug 11, 2009
    #3
  4. asit <> writes:
    > This is the code snippet with main function..
    >
    > #include <stdio.h>
    > #include <pthread.h>

    [snip]
    > When I tried to compile this, it showed the following error ???
    >
    > asit@asit-desktop:~/cpp$ gedit pthread1.c
    > asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c
    > /tmp/ccgO2hyj.o: In function `main':
    > pthread1.c:(.text+0x85): undefined reference to `pthread_create'
    > collect2: ld returned 1 exit status


    Note that <pthread.h> is not a standard C header.

    You need to tell the compiler (actually the linker) where to find the
    library that implements pthreads. The way to do this can vary from
    system to system and from compiler to compiler.

    (For gcc, it's probably either "-pthread" or "-lpthread". Consult
    your system's documentation, perhaps "man pthreads", to be sure.)

    If you have more questions about thread programming, try
    comp.programming.threads.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 11, 2009
    #4
  5. asit

    asit Guest

    Thank you...
     
    asit, Aug 11, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. R Mar
    Replies:
    0
    Views:
    422
    R Mar
    Jan 3, 2004
  2. Avin
    Replies:
    2
    Views:
    15,671
    Jerald Fijerald
    May 8, 2004
  3. lokb
    Replies:
    7
    Views:
    8,795
    red floyd
    Jul 14, 2004
  4. Keith

    Strange Pthread Behavior On IRIX

    Keith, Jun 25, 2003, in forum: C Programming
    Replies:
    1
    Views:
    827
    Keith
    Jun 26, 2003
  5. Dan Cescato
    Replies:
    9
    Views:
    522
    Anthony Baxter
    Oct 6, 2003
Loading...

Share This Page