【发布时间】:2012-03-29 10:23:48
【问题描述】:
我刚刚系统崩溃并重新安装了 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,为什么还是编译不出来?
【问题讨论】:
-
我想你可能想要-pthread,但我忘记了原因。
-
谢谢,我导师的电脑需要添加-pthread,它是lpthread的改进版本,具有线程安全功能。我可以添加 lrt。我认为 lrt 和 pthread 都是 POSIX 的库?
-
@bxshi:POSIX 没有指定库名称;各种 Unix 风格在不同名称的库中都有函数。
-
这个标题回答了我的问题:啊,我需要
-lrt!