[英]Linking Math Library in GCC 4.6.1 (Ubuntu 11.10)
I find a problem in the linking process of my application. 我发现我的应用程序的链接过程中存在问题。 I did not have the same with gcc 4.5.
我对gcc 4.5没有相同的看法。 It tries to link math library with the following command.
它尝试将数学库与以下命令链接。
gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -lm -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals
and report following error massages: 并报告以下错误按摩:
undefined reference to `sqrt'
Any idea ? 任何想法 ?
recent gcc/ld uses the --as-needed linker flag as default. 最近的gcc / ld使用--as-needed链接器标志作为默认值。 Practically, that means libraries have to be specified in the reverse order of dependencies on the command line.
实际上,这意味着必须以与命令行相关的相反顺序指定库。 If the mems_internals library needs the sqrt function your -lm after -lmems_internals.
如果mems_internals库需要sqrt函数,请在-lmems_internals之后使用-lm。
gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals -lm
You didn't tell us what -lmems_internals
is, but maybe the unresolved symbol comes from there. 你没有告诉我们
-lmems_internals
是什么,但也许未解决的符号来自那里。 The order of the -l
options is generally important to the linker, you should always put system libraries last. -l
选项的顺序通常对链接器很重要,您应始终将系统库放在最后。
You can check where the unresolved symbol comes from by using something like 您可以使用类似的方法检查未解析符号的来源
nm yourLibrary | grep sqrt
if there is a U
in front of sqrt
the symbol is undefined. 如果在
sqrt
前面有一个U
,则符号未定义。
I've had the same problem with gcc 4.6.1, even with only one library. 我对gcc 4.6.1也有同样的问题,即使只有一个库。 This doesn't work:
这不起作用:
$ gcc -lm eg.o -o eg
eg.o: In function `foo':
/home/nick/tmp/eg.c:5: undefined reference to `sqrt'
collect2: ld returned 1 exit status
But this does: 但这样做:
$ gcc eg.o -o eg -lm
I hit this because I was using "LDFLAGS=-lm" in my Makefile. 我点击这个是因为我在Makefile中使用了“LDFLAGS = -lm”。 Works fine if you use "LDLIBS=-lm" instead.
如果您使用“LDLIBS = -lm”,则工作正常。
我会说链接器使用了错误的libm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.