使用-lpthread,g ++编译器错误,"未定义引用"信号量调用,例如`sem_open'

Mik*_*ike 4 c++ linux multithreading system-calls undefined-reference

我是posix线程库的新手,我尝试从教程中编译示例代码:

g++ -lpthread agreement.cpp -o agreement
Run Code Online (Sandbox Code Playgroud)

但是我无法编译代码并收到以下错误消息:

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
Run Code Online (Sandbox Code Playgroud)

我知道编译工作需要-lpthread,但是我可能还需要其他任何选项才能解决问题吗?如果不是我如何安装"正确的"pthread库?

谢谢你的帮助!

Ben*_*son 11

你想要编译选项-pthread(如果你真的使用pthreads).如果您只是需要这些功能,librt那么它们就是如此-lrt