8

I have this code:

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

void* cuoco(void* arg)
{
    fprintf(stderr,"Inizio codice cuoco\n");
    fprintf(stderr,"Fine codice cuoco\n");
    return NULL;
}

void* cameriere(void* arg)
{
    fprintf(stderr,"Inizio codice cameriere\n");
    fprintf(stderr,"Fine codice cameriere\n");
    return NULL;
}

void* cliente(void* arg)
{
    fprintf(stderr,"Inizio codice cliente\n");
    fprintf(stderr,"Fine codice cliente\n");
    return NULL;
}

int main(int argc, char* argv[])
{
    void* (*routine)(void*);
    routine=cuoco;
    pthread_t thread_cuoco,thread_cameriere,thread_cliente;
    pthread_create(&thread_cuoco,NULL,routine,NULL);
    return 0;
}

And in the compiler options I insert -lpthread
But it says:
"Undefined reference to pthread_create"
I use ubuntu 10.10, so I already have pthread library installed, I can't figure the reason of this error.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187

5 Answers5

30

Use -lpthread as the last compiler flag.

example: gcc -o sample sample.c -lpthread

stdcall
  • 27,613
  • 18
  • 81
  • 125
  • 1
    @RamyAlZuhouri No you don't. You probably botched the codeblocks settings. +1 to counter wrong downvote. – cnicutar Mar 21 '12 at 11:05
  • Yes, I have put it.It's on settings->compiler and debugger->(compiler settings tab)->other options. – Ramy Al Zuhouri Mar 21 '12 at 11:21
  • 3
    @RamyAlZuhouri: That is not right. It is more of a linker setting. So do this: settings->compiler and debugger->linker settings (tab). Select "Add" under Link libraries section. Add the path to pthread library (most likely /usr/lib/libpthread.so). Try building then – another.anon.coward Mar 21 '12 at 11:31
  • Plus you should not downvote if you tried it, but it didn't work, but you didn't tell it us either... –  May 16 '12 at 12:42
  • 2
    Note that the use of `-pthread` is preferable, because `-lpthread` will fail to link on a system with only `libpthread.a` installed. – e-sushi Apr 22 '15 at 20:37
15

Without seeing the compiler command, I suspect -lpthread is not at end. Libraries need to be placed at end of the compiler command:

gcc main.c -lpthread

However, use -pthread instead of -lpthread, as -pthread may add other settings (like defining the macro _REENTRANT for example).

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I compile it with code::blocks, in the C::B options now I changed _-lpthread_ with _-pthread_, but nothing has changed. This is the only options I have in settings. – Ramy Al Zuhouri Mar 21 '12 at 11:02
  • I am unfamiliar with code::blocks, but can you attempt a complete rebuild of the source? – hmjd Mar 21 '12 at 11:03
5

Use the following command:

gcc -pthread -o main main.c

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
AbdulFattah Popoola
  • 949
  • 2
  • 13
  • 22
0

In Eclipse, you should add string pthread.

Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC Linker -> Libraries -> Libraries (-l) -> Add -> pthread

After this, you can Build your project.

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
ognjenkl
  • 1,407
  • 15
  • 11
-2

found the solution guys :D just go to settings >> compiler >> linker tab >>add lib

go to drive and go to lib folder and find x86_64_linux_gnu and find pthread enjoy :)

Jared Forth
  • 1,577
  • 6
  • 17
  • 32
AFsoft
  • 1