使用-lpthread,g ++编译器错误,“未定义引用到”信号量调用,如`sem_open' [英] with -lpthread, g++ compiler error, "undefined reference to " semaphore calls such as `sem_open'

查看:1116
本文介绍了使用-lpthread,g ++编译器错误,“未定义引用到”信号量调用,如`sem_open'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  g ++  -  posix线程库我是posix线程库的新手,我试图从教程中编译示例代码: lpthread agreement.cpp -o agreement 

但是我无法编译代码并得到以下错误message:

  a3q2.cpp :(。text + 0x7e):未定义引用`sem_open'
a3q2.cpp (。text + 0xab):未定义引用`sem_wait'
a3q2.cpp :(。text + 0x290):未定义引用`sem_post'
a3q2.cpp :(。未定义引用`sem_close'
a3q2.cpp :(。text + 0x2bb):未定义引用`sem_unlink'
collect2:ld返回1退出状态
make:*** [a3q2_exe]错误1

我知道-lpthread是编译工作所需要的,我可能需要解决问题?如果没有,我如何安装正确的pthread库?



感谢您的帮助!

解决方案

你想要编译选项 -pthread (如果你真的使用pthreads)。如果你只需要那些函数,他们在 librt 所以使用 -lrt


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

这篇关于使用-lpthread,g ++编译器错误,“未定义引用到”信号量调用,如`sem_open'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆