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/9923495/

相关文章:

c - malloc + fgets 动态分配

c - 我正在尝试编写一个在 C 中定义为参数的函数

linux - 如何在 BASH 中按创建日期对目录进行排序?

windows - Windows 的 cat 命令

linux - 如何设置我的 Linux X 终端以便 Emacs 可以访问 256 色?

c - 检测 Control 键何时被释放

C- - 如何对目录中的所有文件执行函数?

C程序找出数字最多不同的数字

c++ - Linux,C++,使用 poll/select 等待事件(不阻塞执行线程)

php - 我应该使用什么过程来构建 linux 配置页面?