C语言 Ubuntu 11.10 上的 c 数学链接器问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7824439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:56:01  来源:igfitidea点击:

c math linker problems on Ubuntu 11.10

cgccubuntulinkermath.h

提问by Hachi

Some strange error appeared after I upgraded my Ubuntu from (10.11, 11.04 i dont know)to 11.10.

我将 Ubuntu 从升级(10.11, 11.04 i dont know)到 11.10后出现了一些奇怪的错误。

I am getting an undefined reference to 'sqrt'while using math.h and linking with -lm

我正在undefined reference to 'sqrt'使用 math.h并与 -lm 链接

I'm compiling with gcc -Wall -Werror -g -Iinclude/ -lm lib/matrix.c src/analyse.c -o bin/analyse.oboth source-files use and include math.h.

我正在使用gcc -Wall -Werror -g -Iinclude/ -lm lib/matrix.c src/analyse.c -o bin/analyse.o源文件进行编译并包含 math.h。

This code compiled without problems for and I didn't change much since the upgrade but now it won't work.

这段代码编译没有问题,升级后我没有太大变化,但现在它不起作用了。

Do you have any suggestions what I can do, to find the error?

你有什么建议我可以做什么,找到错误?

I'm sorry, if this question was asked before; there are so many posts on math linker errors and I didn't find a matching one

对不起,如果这个问题以前被问过;有很多关于数学链接器错误的帖子,但我没有找到匹配的

回答by user786653

The library that you are using needs to be placed after the files that use it when you are using it from the command line. So place -lmon after your C files on the command line.

当您从命令行使用它时,您正在使用的库需要放在使用它的文件之后。所以放在-lm命令行上的 C 文件之后。

Reference

参考

回答by quido.speedy

SOLVED, this is not the common missing -lmproblem! I'm in the same situation after upgrade to (k)ubuntu 11.10!

已解决,这不是常见的缺失-lm问题!升级到 (k)ubuntu 11.10 后,我处于同样的情况!

$ whereis math.h
math: /usr/include/math.h

Makefile:
CC=gcc
CFLAGS=--std=c99 -g -pedantic -Wall -lm

uname:
Linux idefix 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

You really HAVE TO place the -lmswith after -o foo foo.cparameter

你真的必须在-o foo foo.c参数之后放置-lm开关

Output:
pidi@idefix:~/projekt1$ make
gcc -o b1 b1.c --std=c99 -g -pedantic -Wall -lm
pidi@idefix:~/projekt1$

So swap your flags in Makefile! GUYS. This is pretty new (and serious) BUG!

所以在 Makefile 中交换你的标志!伙计们。这是一个相当新(而且很严重)的 BUG!

回答by Seldon_SL

This is a problem due to the default activation of the gcc flag --as-neededin the linker

这是由于--as-needed链接器中gcc 标志的默认激活导致的问题

More information here: http://www.gentoo.org/proj/en/qa/asneeded.xml

更多信息请访问:http: //www.gentoo.org/proj/en/qa/asneeded.xml

Simple fix (worked for me at least):

简单的修复(至少对我有用):

Add -Wl,--no-as-neededto the linker

添加-Wl,--no-as-needed到链接器

回答by Diego

I found the same problem after upgrading my Ubuntu to 11.10 version. I use Netbeans for developing and solved the problem by specifying the "Mathematics" standard library as it follows:

将我的 Ubuntu 升级到 11.10 版本后,我发现了同样的问题。我使用 Netbeans 通过指定“数学”标准库来开发和解决问题,如下所示:

Right click on project, click on Properties, select "Linker" on menu, click on "Libraries" and then "Add Standard Library" choosing "Mathematics".

右键单击项目,单击属性,在菜单上选择“链接器”,单击“库”,然后单击“添加标准库”,选择“数学”。

When compiling the '-lm' option is placed after all the other options and it works. Probably this gcc version follows a specific architecture and it expects the libraries at the end of the command compiling line.

编译时,'-lm' 选项放在所有其他选项之后,它可以工作。可能这个 gcc 版本遵循特定的架构,它期望在命令编译行的末尾有库。

Cheers!

干杯!

D.

D.

回答by user2485132

cc filename.c -lm

just try..........?

你试一试..........?