简体   繁体   English

sem_open()错误:linux上的“对sem_open()的未定义引用”(Ubuntu 10.10)

[英]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.

相关问题 了解 sem_open() - Understanding the sem_open() sem_open 在 linux 下返回 0 - sem_open returns 0 under linux 使用-lpthread,g ++编译器错误,“未定义引用”信号量调用,例如`sem_open&#39; - with -lpthread, g++ compiler error, “undefined reference to ” semaphore calls such as `sem_open' C ++ Macs OS X semaphore.h:出现“ sem_open()”和“ sem_wait()”问题 - C++ Macs OS X semaphore.h: trouble with `sem_open()` and `sem_wait()` 我可以在以下程序中使用sem_open,但是我看到崩溃了吗? - Can I use sem_open in the following program, however I see a crash here? 未定义对符号&#39;sem_close @@ GLIBC_2.2.5&#39;的引用 - undefined reference to symbol 'sem_close@@GLIBC_2.2.5' sem_wait()无法在Linux上唤醒 - sem_wait() failed to wake up on linux 错误:对“sqlite3_open”的未定义引用 - Error: undefined reference to `sqlite3_open' 在Ubuntu上的Netbeans C ++中,Sqlite未定义对“ sqlite3_open”错误的引用,将SQLite集成到Netbeans C ++ Ubuntu中 - Sqlite undefined reference to `sqlite3_open' error in Netbeans C++ on Ubuntu, Integrating SQLite into Netbeans C++ Ubuntu sem_post(sem_t * sem)和sem_wait(sem_t * sem)周围是否存在完整的内存屏障? - Is there a full memory barrier around sem_post(sem_t * sem) and sem_wait(sem_t * sem)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM