我正在尝试使用共享内存制作一个简单的通信器。
它应该能够写入一些文本并将其保存在共享内存中。
有人可以向我解释为什么我会出错:
对“shm_open”
collect2 的未定义引用:错误:ld 返回 1 个退出状态
我试图为两个程序创建 Makefile 以进行编译,但我仍然遇到错误。
这是我现在正在使用的代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#define TEXT_SIZE 512
int i;
struct shared_data
{
int data;
char some_text[TEXT_SIZE];
};
int main()
{
struct shared_data *ptr;
const char *shm_name = "comunicator";
int shm_fd;
shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, sizeof(struct shared_data));
ptr = mmap(0, sizeof(struct shared_data), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr …Run Code Online (Sandbox Code Playgroud)