C 未定义的引用';shm#U open';,已在此处添加-轻轨标志
我刚刚遇到系统崩溃,重新安装了Ubuntu11.10,我的代码产生了这个奇怪的错误 我编写了一个简单的代码示例来测试问题所在:C 未定义的引用';shm#U open';,已在此处添加-轻轨标志,c,linux,C,Linux,我刚刚遇到系统崩溃,重新安装了Ubuntu11.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
#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);
}
我已经添加了-lrt-lib,为什么它还没有编译?最后的库:
gcc试验.轻铁c-o试验
发件人:
-图书馆
-图书馆
链接时搜索名为library的库。
(第二种选择是将库作为单独的参数
仅适用于POSIX合规性,不建议使用。)
在命令中写入此选项的位置会有所不同;
链接器在中搜索和处理库和对象文件
指定它们的顺序。
因此,`foo.o-lzbar.o'在文件foo.o之后搜索库`z',但是
在酒吧前。如果bar.o引用“z”中的函数,则这些函数
可能无法加载。
将编译行从
gcc -lrt test.c -o test
到
在
专家C编程中
第108页:
放置库选项的位置:始终将-l库选项放置在编译命令行的最右端。
但它不知道为什么,所以我想这有点规则:)/P>我想在中间添加一些库也是有效的。只是好奇为什么
gcc
把顺序看得如此重要?这次我需要在这个gcc命令的末尾添加lib,但是在我重新安装linux之前,我可以在gcc
之后添加lib,它也可以编译。怎么会这样?有任何配置文件或变量吗?@PavanManjunath:较新的GCC版本接受命令行中任意位置的库,并且以任何顺序接受。较老的版本要求它们排在最后并按依赖顺序列出(因此任何需要librt
的内容都会排在它前面)。@PavanManjunath:它必须这样做。搜索库以解析未定义的符号。如果顺序无关紧要,那么当您要搜索的两个或多个库中定义了所需符号时,将无法控制使用了哪个库。@R..:my GCC(Debian 4.4.5-8)将OP的程序与给定的命令行链接得很好。我不确定这个特性是什么时候引入的,以及它是如何实现的,但以前它引发了很多这样的问题。gcc是否总是需要在末尾添加lib?我没有更改代码和Makefile,但在重新安装计算机之前,它工作正常。我想您可能需要-pthread,但我忘记了原因。谢谢,我导师的计算机需要添加-pthread,它是带有线程安全功能的lpthread的改进版本。我可以加上轻轨。我认为lrt和pthread都是POSIX的库?@bxshi:POSIX没有指定库名;各种Unix风格在不同名称的库中都有函数。这个标题回答了我的问题:啊,我需要-lrt
!
-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.
gcc -lrt test.c -o test
gcc test.c -o test -lrt