I just have a system crash and reinstall Ubuntu 11.10, and my code produces this strange error.
我有一个系统崩溃和重新安装Ubuntu 11.10,我的代码产生了这个奇怪的错误。
I wrote a simple code sample to test where the problem is:
我编写了一个简单的代码示例来测试问题所在:
#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);
}
and the compile command is
编译命令是
gcc -lrt test.c -o test
gcc轻轨车测试。c - o测试
The error is:
错误的是:
/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
I have already added -lrt lib, why does it still not compile?
我已经添加了-lrt lib,为什么它还没有编译?
3 个解决方案
#1
43
Libraries at the end:
最后库:
gcc test.c -o test -lrt
gcc测试。c - o测试轻轨车
From GCC Link Options:
从GCC链接选项:
-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.
#2
4
In Expert C programming
Page 108: <Handy Heuristic> Where to Put Library Options:Always put the -l library options at the rightmost end of your compilation command line.
But it doesn't tell why, so i guess this is somewhat a rule?:)
在专家C编程页108: <便利的启发式> ,在哪里放置库选项:总是把-l库选项放在编译命令行最右边。但它并没有说明原因,所以我想这是一个规则吧?
#3
3
Change the compile line from
将编译行从
gcc -lrt test.c -o test
to
来
gcc test.c -o test -lrt
#1
43
Libraries at the end:
最后库:
gcc test.c -o test -lrt
gcc测试。c - o测试轻轨车
From GCC Link Options:
从GCC链接选项:
-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.
#2
4
In Expert C programming
Page 108: <Handy Heuristic> Where to Put Library Options:Always put the -l library options at the rightmost end of your compilation command line.
But it doesn't tell why, so i guess this is somewhat a rule?:)
在专家C编程页108: <便利的启发式> ,在哪里放置库选项:总是把-l库选项放在编译命令行最右边。但它并没有说明原因,所以我想这是一个规则吧?
#3
3
Change the compile line from
将编译行从
gcc -lrt test.c -o test
to
来
gcc test.c -o test -lrt