c - undefined reference 'shm_open' ,已在此处添加 -lrt 标志

标签 c linux

我刚刚遇到系统崩溃并重新安装 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);
}

编译命令是

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

我已经添加了-lrt lib,为什么还是编译不了?

最佳答案

最后的库:

gcc test.c -o test -lrt

来自GCC Link Options :

-llibrary
-l library
    Search the library named library when linking. 
    (The second alternative with the library as a separate argument
    is only for POSIX compliance and is not recommended.)

    It makes a difference where in the command you write this option;
    the linker searches and processes libraries and object files in the
    order they are specified.
    Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but
    before bar.o. If bar.o refers to functions in `z', those functions
    may not be loaded.

关于c - undefined reference 'shm_open' ,已在此处添加 -lrt 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212494/

相关文章:

c - 在C中查找二维数组中最长的字符串

linux - 如何部署使用英特尔 IPP 的应用程序?

c - 通过 TCP 连接使用 Berkeley 套接字传输可变结构大小

linux - 配置我的 Web 服务器以将单独的文件夹公开为单独的 Web 服务器?

arrays - 在 bash 中覆盖二维数组单元格

C flash文件系统使用fwrite生命周期有限制吗?

无法将字符串从 Ruby 传递到我的 Rust 函数中

c - 当您使用负参数调用 glClearColor 时会发生什么?

c - 使用 Windows 10 和 C (Visual Studio 2015) 进行套接字编程

c++ - 在一个线程上删除具有数百万个字符串的大型 HashMap 会影响另一个线程的性能