【问题标题】:Undefined reference to `shm_open' even while compiling with -pthread -lrt即使在使用 -pthread -lrt 编译时,对 `shm_open' 的未定义引用
【发布时间】:2017-03-27 16:43:38
【问题描述】:

我看到有人提出了类似的问题,但没有一致的解决方案。 在 linux 上使用 gcc 编译器,我得到消息:

/tmp/ccPNsJFZ.o: In function `main':
testChTh.c:(.text+0xbfb): undefined reference to `shm_open'
collect2: error: ld returned 1 exit status

在命令行中输入gcc -pthread -lrt -o testChTh testChTh.c 后。我在我的代码中只使用了一次 shm_open,即:

int shm_fd;

  /* create the shared memory segment */
  shm_fd = shm_open(name, O_RDWR, 0666);

我包含以下相关库:

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

非常感谢任何见解!

【问题讨论】:

    标签: c gcc


    【解决方案1】:

    你的情况

      gcc -pthread -lrt -o testChTh testChTh.c
    

    不会工作,因为您最后提供了testChTh.c,它希望使用librt。你需要像这样写

     gcc -o testChTh testChTh.c -pthread -lrt
    

    引用online manual(强调我的)

    -l library

    在命令中编写此选项会有所不同;链接器会按照指定的顺序搜索和处理库和目标文件。因此,“foo.o -lz bar.o”在文件 foo.o 之后但在 bar.o 之前搜索库“z”。如果 bar.o 引用了 ‘z’ 中的函数,这些函数可能不会被加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 2014-04-27
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多