鉴于此代码:
#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);
}
我从日食undefined reference to sem_open
得到。
我已经检查了这个post,因为这个问题与我的问题非常相似,但我不明白我的错误究竟在哪里,
请您解释一下我需要修复它/添加另一个编译命令(如果确实如此)?
非常感谢
答案 0 :(得分:3)
编译时需要包含-lpthread
。链接器使用它来将二进制文件与库链接。
其他答案已经介绍了如何在命令行中执行此操作。
要在Eclipse中执行此操作,您需要按照here的说明进行操作:
在项目属性中,转到:C / C ++ Build - &gt;设置。然后 在“工具设置”中,选择“链接器”下的“库”。你可以添加所有 你的项目库(没有“-l”)。也在较低的 另外,您可以为搜索库添加自定义路径
答案 1 :(得分:1)
链接时,必须将标志-pthread
或-lrt
添加到命令行。它正好在manual page。
答案 2 :(得分:1)
正如您在链接的问题的第一个答案中所写的那样,您需要以这种方式编译它:
gcc source.c -lpthread
-lrt
或-pthread
也会这样做。