C语言 对 sqrt(或其他数学函数)的未定义引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5248919/
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
undefined reference to sqrt (or other mathematical functions)
提问by Waypoint
I have this simple code:
我有这个简单的代码:
max = (int) sqrt (number);
and in the header I have:
在标题中我有:
#include <math.h>
But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.
但是应用程序仍然说对sqrt. 你看这里有什么问题吗?看起来一切都应该没问题。
回答by paxdiablo
You may find that you have to link with the math libraries on whatever system you're using, something like:
您可能会发现您必须在使用的任何系统上链接数学库,例如:
gcc -o myprog myprog.c -L/path/to/libs -lm
^^^ - this bit here.
Including headers lets a compiler know about function declarations but it does notnecessarily automatically link to the code required to perform that function.
包括标题让编译器知道函数的声明,但它并没有必然自动链接到所需的代码来执行该功能。
Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).
否则,您需要向我们展示您的代码、编译命令和运行的平台(操作系统、编译器等)。
The following code compiles and links fine:
以下代码编译并链接正常:
#include <math.h>
int main (void) {
int max = sqrt (9);
return 0;
}
Just be aware that somecompilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.
请注意,某些编译系统取决于在命令行上给出库的顺序。我的意思是,他们可以按顺序处理库,并且只使用它们来满足序列中那个点未解析的符号。
So, for example, given the commands:
因此,例如,给定命令:
gcc -o plugh plugh.o -lxyzzy
gcc -o plugh -lxyzzy plugh.o
and plugh.orequires something from the xyzzylibrary, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.
并且plugh.o需要从xyzzy库中获取一些东西,第二个可能无法按您的预期工作。在您列出库时,没有未解决的符号需要满足。
And when the unresolved symbols from plugh.odoappear, it's too late.
当从解析的符号plugh.o做出现,为时已晚。
回答by krtek
I suppose you have imported math.h with #include <math.h>
我想你已经导入了 math.h #include <math.h>
So the only other reason I can see is a missing linking information. You must link your code with the -lmoption.
所以我能看到的唯一其他原因是缺少链接信息。您必须将代码与-lm选项链接。
If you're simply trying to compile one file with gcc, just add -lmto your command line, otherwise, give some informations about your building process.
如果你只是想用 gcc 编译一个文件,只需添加-lm到你的命令行,否则,提供一些关于你的构建过程的信息。
回答by Hassan Rahman
Just adding the #include <math.h>in c source file and -lm in Makefile at the end will work for me.
只需在最后添加#include <math.h>in c 源文件和 Makefile 中的 -lm 即可。
gcc -pthread -o p3 p3.c -lm
回答by Achal
Here are my observation, firstly you need to include the header math.has sqrt()function declared in math.hheader file. For e.g
这是我的观察,首先您需要将头文件math.h作为sqrt()在math.h头文件中声明的函数包含在内。例如
#include <math.h>
secondly, if you read manual page of sqrtyou will notice this line Link with -lm.
其次,如果您阅读sqrt 的手册页,您会注意到这一行Link with -lm。
#include <math.h> /* header file you need to include */
double sqrt(double x); /* prototype of sqrt() function */
Link with -lm. /* Library linking instruction */
But application still says undefined reference to sqrt. Do you see any problem here?
但是应用程序仍然说对 sqrt 的未定义引用。你看这里有什么问题吗?
Compiler error is correct as you haven't linked your program with library lm& linker is unable to find reference of sqrt(), you need to link it explicitly. For e.g
编译器错误是正确的,因为您没有将您的程序与库lm链接,并且链接器无法找到 的引用sqrt(),您需要明确地链接它。例如
gcc -Wall -Wextra -Werror -pedantic test.c -lm
回答by Mohamed Mnete
I had the same issue, but I simply solved it by adding -lm after the command that runs my code. Example. gcc code.c -lm
我遇到了同样的问题,但我只是通过在运行我的代码的命令之后添加 -lm 来解决它。例子。gcc 代码.c -lm

