sem_open 不适用于 Ubuntu:未定义对 `sem_open' 的引用 [英] sem_open doesn't work with Ubuntu : undefined reference to `sem_open'

查看:31
本文介绍了sem_open 不适用于 Ubuntu:未定义对 `sem_open' 的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此代码:

#include #include #include #include #include #include #include #include #include #include #include #define BUF_SIZE 256int main(){key_t 键;char *virtualaddr;sem_t *get, *put;int shmid;const char* messageOne = "Hello world ,我是 1 号孩子\n";const char* messageTwo = "Hello world,我是 2 号孩子\n";字符缓冲区[BUF_SIZE];key = ftok("任意文件",'R');shmid = shmget(key,1024,0644|IPC_CREAT);......shmctl (shmid, IPC_RMID, NULL);退出(退出成功);}

我从 Eclipse undefined reference to sem_open 得到.

我已经检查过这个 post 因为这个问题和我的很相似,但不明白我的错误到底在哪里,

您能否解释一下我需要在哪里修复它/添加另一个编译命令(如果确实如此)?

非常感谢

解决方案

编译时需要包含-lpthread.链接器使用它来将您的二进制文件与库链接.

其他答案已经涵盖了如何在命令行上执行此操作.

要在 Eclipse 中执行此操作,您需要按照此处的说明进行操作:<块引用>

在项目属性中,转到:C/C++ Build --> Settings.然后工具设置",选择链接器"下的库".您可以添加所有你的项目库在那里(没有-l").还有在下部分,您可以添加自定义路径到搜索库

Given this code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <pthread.h>

#define BUF_SIZE 256
int main()
{
    key_t key;
    char *virtualaddr;
    sem_t *get, *put;
    int shmid;

   const char* messageOne = "Hello world , I'm child number 1\n";
   const char* messageTwo = "Hello world , I'm child number 2\n";

   char buf[BUF_SIZE];

   key = ftok("anyfile",'R');

  shmid = shmget(key,1024,0644|IPC_CREAT);
...
...
shmctl (shmid, IPC_RMID, NULL);
exit(EXIT_SUCCESS);
}

I get from eclipse undefined reference to sem_open .

I've check with this post since this question is very similar to mine , but didn't understand exactly where is my mistake ,

Can you please explain where do I need to fix it / add another compilation command (if this is indeed the case) ?

Much appreciated

解决方案

You need to include -lpthread when compiling. This is used by the linker, to link your binary against the library.

The other answers already cover how to do this on the command line.

To do this in Eclipse, you need to follow the directions here:

In the project properties, go to : C/C++ Build --> Settings. Then "Tool Settings", select "Libraries" under "Linker". You can add all your project libraries there (without the "-l"). Also in the lower part, you can add custom path to search libraries

这篇关于sem_open 不适用于 Ubuntu:未定义对 `sem_open&amp;#39; 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆