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