Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have recently installed XUbuntu 11.10 64bit, but I am having problem compiling the most simple pthread example.

Here is the code pthread_simple.c:

#include <stdio.h>
#include <pthread.h> 
main()  {
  pthread_t f2_thread, f1_thread; 
  void *f2(), *f1();
  int i1,i2;
  i1 = 1;
  i2 = 2;
void *f1(int *x){
  int i;
  i = *x;
  printf("f1: %d",i);
void *f2(int *x){
  int i;
  i = *x;
  printf("f2: %d",i);

And here is the compile command

gcc -lpthread pthread_simple.c

The results:

lptang@tlp-linux:~/test/test-pthread$ gcc -lpthread pthread_simple.c 
/tmp/ccmV0LdM.o: In function `main':
pthread_simple.c:(.text+0x2c): undefined reference to `pthread_create'
pthread_simple.c:(.text+0x46): undefined reference to `pthread_create'
pthread_simple.c:(.text+0x57): undefined reference to `pthread_join'
pthread_simple.c:(.text+0x68): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

Does anyone know what's causing the problem?

share|improve this question
Is it stackexchange's fault that you have empty includes in the first two lines? There should be an #include <pthread.h> –  Frg Mar 4 '12 at 13:24
Yes, I used the pre environment. It should now display correctly. –  chtlp Mar 4 '12 at 13:27
Tried gcc -pthread...? –  sr_ Mar 4 '12 at 13:42
BTW, please compile with -Wall, you're missing headers. (And sr_ is correct.) –  Mat Mar 4 '12 at 13:43
Thanks. I changed -lpthread to -pthread and it worked. –  chtlp Mar 4 '12 at 13:51

1 Answer 1

up vote 10 down vote accepted

In the latest versions of gcc compiler require that libraries follow the object or source files.

So to compile this it should be:

gcc pthread_sample.c -lpthread

Normally though pthread code is compiled this way:

gcc -pthread pthread_sample.c
share|improve this answer
Thanks, it does the trick! –  chtlp Mar 4 '12 at 13:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.