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

故事扮演 提交于 2019-12-23 13:21:34

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/11111291/with-lpthread-g-compiler-error-undefined-reference-to-semaphore-calls-su

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!