with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'

Mike picture Mike · Jun 20, 2012 · Viewed 14.6k times · Source

I am new to posix thread library, and I tried to compile a sample code from a tutorial with:

g++ -lpthread agreement.cpp -o agreement

however I was not able to compile the code and got the following error message:

a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'
a3q2.cpp:(.text+0xab): undefined reference to `sem_wait'
a3q2.cpp:(.text+0x290): undefined reference to `sem_post'
a3q2.cpp:(.text+0x2af): undefined reference to `sem_close'
a3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [a3q2_exe] Error 1

I am aware that -lpthread is needed for compilation to work, but is there any other options i might need to solve the problem? if not how do I have to install the "proper" pthread library?

Thanks for your help!

Answer

Ben Jackson picture Ben Jackson · Jun 20, 2012

You want the compile option -pthread (if you are really using pthreads). If you just need those functions they are in librt so use -lrt