繁体   English   中英

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

[英]sem_open() error: “undefined reference to sem_open()” on linux (Ubuntu 10.10)

所以我收到错误:“对sem_open()的未定义引用”,即使我已经包含了semaphore.h头文件。 我的所有pthread函数调用(mutex,pthread_create等)都发生了同样的事情。 有什么想法吗? 我使用以下命令编译:

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。

包括标题不会告诉ld关于库。 您需要将-lrt添加到编译命令行。 对于线程,您需要-lpthread或-pthread,具体取决于您的平台。

该库不是标题。 标题不是库。 这是一个重要的区别。 请参阅头文件和库之间的区别是什么?

Ubuntu中的工作选项是-lpthread 但是如果你在suse或其他系统上工作,正确的选项是-lrt 另外, Linux Programmin Interface一书提到了-lrt作为正确的选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM