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

Rob*_*bin 13 c++ linux semaphore

所以我收到错误:"对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;
}
Run Code Online (Sandbox Code Playgroud)

Vla*_*d H 19

您需要使用-lpthread选项链接到pthread lib .


Wil*_*ell 6

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

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