Linux 使用 -lpthread、g++ 编译器错误、“未定义引用”信号量调用,例如 `sem_open'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11111291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'
提问by Mike
I am new to posix thread library, and I tried to compile a sample code from a tutorial with:
我是 posix 线程库的新手,我尝试从教程中编译示例代码:
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?
我知道需要 -lpthread 编译才能工作,但是我可能需要其他任何选项来解决问题吗?如果不是,我该如何安装“正确的”pthread 库?
Thanks for your help!
谢谢你的帮助!
采纳答案by Ben Hymanson
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
您需要编译选项-pthread
(如果您确实在使用 pthreads)。如果您只需要它们librt
使用的那些功能-lrt