[英]sem_open() error: “undefined reference to sem_open()” on linux (Ubuntu 10.10)
So I am getting the error: "undefined reference to sem_open()" even though I have include the semaphore.h header. 所以我收到错误:“对sem_open()的未定义引用”,即使我已经包含了semaphore.h头文件。 The same thing is happening for all my pthread function calls (mutex, pthread_create, etc).
我的所有pthread函数调用(mutex,pthread_create等)都发生了同样的事情。 Any thoughts?
有什么想法吗? I am using the following command to compile:
我使用以下命令编译:
g++ '/home/robin/Desktop/main.cpp' -o '/home/robin/Desktop/main.out' g ++'/ home / rbin /Desktop/main.cpp'-o'/ home / robin /Desktop / main.out'
#include <iostream>
using namespace std;
#include <pthread.h>
#include <semaphore.h>
#include <fcntl.h>
const char *serverControl = "/serverControl";
sem_t* semID;
int main ( int argc, char *argv[] )
{
//create semaphore used to control servers
semID = sem_open(serverControl,O_CREAT,O_RDWR,0);
return 0;
}
您需要使用-lpthread
选项链接到pthread lib。
Including the header does not tell ld about the library. 包括标题不会告诉ld关于库。 You need to add -lrt to your compilation command line.
您需要将-lrt添加到编译命令行。 For threading, you need either -lpthread or -pthread, depending on your platform.
对于线程,您需要-lpthread或-pthread,具体取决于您的平台。
The library is not the header. 该库不是标题。 The header is not the library.
标题不是库。 This is an important distinction.
这是一个重要的区别。 See What's the difference between a header file and a library?
请参阅头文件和库之间的区别是什么?
The working option in Ubuntu is -lpthread . Ubuntu中的工作选项是-lpthread 。 But if you work on suse or other systems the correct option is -lrt .
但是如果你在suse或其他系统上工作,正确的选项是-lrt 。 Also the book Linux Programmin Interface mentions -lrt as the correct option.
另外, Linux Programmin Interface一书提到了-lrt作为正确的选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.