相关疑难解决方法(0)

什么是未定义的引用/未解析的外部符号错误,我该如何解决?

什么是未定义的参考/未解决的外部符号错误?什么是常见原因以及如何修复/预防它们?

随意编辑/添加您自己的.

c++ c++-faq linker-errors unresolved-external undefined-reference

1418
推荐指数
32
解决办法
52万
查看次数

未定义的引用'shm_open',已在此处添加-lrt标志

我只是系统崩溃并重新安装Ubuntu 11.10,我的代码产生了这个奇怪的错误.

我写了一个简单的代码示例来测试问题所在:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

int main (void) {

    int i;

    i = shm_open ("/tmp/shared", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);   printf ("shm_open rc = %d\n", i);

    shm_unlink ("/tmp/shared");

    return (0);
}
Run Code Online (Sandbox Code Playgroud)

并且编译命令是

gcc -lrt test.c -o test

错误是:

/tmp/ccxVIUiP.o: In function `main':
test.c:(.text+0x21): undefined reference to `shm_open'
test.c:(.text+0x46): undefined reference to `shm_unlink'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

我已经添加了-lrt lib,为什么它仍然没有编译?

c linux

34
推荐指数
2
解决办法
6万
查看次数

对“shm_open”的未定义引用

我正在尝试使用共享内存制作一个简单的通信器。

它应该能够写入一些文本并将其保存在共享内存中。

有人可以向我解释为什么我会出错:

对“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)

c linux shared-memory

4
推荐指数
1
解决办法
9413
查看次数