简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM