Linux : Undefined reference to pthread_create

0 votes

So, I tried to pick up the following demo off of web;

#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);
      }
   }
   pthread_exit(NULL);
}


When I try to compile it on Ubuntu 9.04, following error gets thrown at me:

corey@ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main’:
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status


Any idea what im doing wrong?

May 29 in Linux Administration by Upasana
• 8,530 points
581 views

1 answer to this question.

0 votes

Try this;

gcc -pthread -o term term.c
answered May 29 by Shubham
• 13,300 points

Related Questions In Linux Administration

–1 vote
1 answer

How to get octal file permission in linux?

You can use this: stat -c "%a %n" ...READ MORE

answered Jan 3 in Linux Administration by Omkar
• 67,660 points
109 views
0 votes
1 answer

How to create a symlink in Linux?

To create a symbolic link, you can ...READ MORE

answered Jan 31 in Linux Administration by Omkar
• 67,660 points
73 views
0 votes
1 answer

set permanent path to Linux

Add it to your ~/.profile or ~/.bashrc file: export PATH="$PATH:/path/to/dir" You may ...READ MORE

answered Feb 27 in Linux Administration by DareDev
• 6,810 points
33 views
0 votes
1 answer

How to find path of software installed in Linux?

There are multiple ways to find the ...READ MORE

answered May 19 in Linux Administration by Rishi
118 views
0 votes
1 answer
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,240 points
91 views
0 votes
1 answer

Making a program sleep for milliseconds?

There are no standard C API's that ...READ MORE

answered Mar 1 in Linux Administration by DareDev
• 6,810 points
37 views
0 votes
1 answer

Install postgreSQL on Ubuntu

Installing PostgreSQL on Ubuntu is very simple, ...READ MORE

answered Mar 22 in Database by Nitesh
60 views
0 votes
1 answer

Removing a symlink to a directory

rm foo try this. Also, You need write ...READ MORE

answered May 15 in Linux Administration by Shubham
• 13,300 points
30 views
0 votes
1 answer

Writing a heredoc to a file in a script

try to use tee: tee newfile <<EOF line 1 line 2 line ...READ MORE

answered May 20 in Linux Administration by Shubham
• 13,300 points
57 views