pthread_create problem in POSIX, undefined reference to 'pthread_create'

Hi guys, I have this problem in my compiler, I'm using codeblock..

this is the code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}


the error is.. undefined reference to 'pthread_create'.....

Why is happeing this!!!??
You need stdlib.h for exit().

You need to link with libpthread.
c++ x.cpp -o x -lpthread
IIRC you need to compile (preprocessor) and link with the -pthread flag
Thanks guys, I fix the code with c++ x.cpp -o x -lpthread...in the console all code is working it...


BUT!!! Code::Block is Still lauching errors.... Thank you! I'm Thankful
Topic archived. No new replies allowed.