与-lpthread,克++编译器错误,“未定义提及”旗语呼叫如`sem_open”(with -l

2019-09-18 06:36发布

我是新来的POSIX线程库,我试图从一个教程编译的代码示例:

g++ -lpthread agreement.cpp -o agreement

然而,我无法编译代码,并得到了以下错误信息:

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

据我所知,-lpthread需要编译工作,但有我可能需要解决的问题任何其他的选择吗? 如果没有怎么办我必须安装“正确”的并行线程库?

谢谢你的帮助!

Answer 1:

你想编译选项-pthread (如果你真的使用并行线程)。 如果你只需要那些他们在功能librt所以用-lrt



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