对 sqrt(或其他数学函数)的未定义引用
我有这个简单的代码:
max = (int) sqrt (number);
在标头中我有:
#include <math.h>
但应用程序仍然显示对 sqrt
的未定义引用。你看到这里有什么问题吗?看起来一切都应该没问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能会发现,无论您使用什么系统,都必须链接到数学库,例如:
包含标头可以让编译器了解函数声明,但它不一定自动链接到所需的代码来执行该功能。
如果做不到这一点,您需要向我们展示您的代码、编译命令和您正在运行的平台(操作系统、编译器等)。
以下代码可以正常编译和链接:
请注意,某些编译系统取决于命令行上给出的库的顺序。我的意思是,他们可以按顺序处理库,并且仅使用它们来满足序列中该点的未解析符号。
因此,例如,给定命令:
和plugh.o 需要
xyzzy
库中的某些内容,第二个可能无法按您的预期工作。在列出库时,没有需要满足的未解析符号。当来自
plugh.o
do 的未解析符号出现时,为时已晚。You may find that you have to link with the math libraries on whatever system you're using, something like:
Including headers lets a compiler know about function declarations but it does not necessarily 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:
Just be aware that some compilation 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:
and
plugh.o
requires something from thexyzzy
library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.And when the unresolved symbols from
plugh.o
do appear, it's too late.我想您已经使用
#include
导入了 math.h,因此我能看到的唯一其他原因是缺少链接信息。您必须使用
-lm
选项链接您的代码。如果您只是尝试使用 gcc 编译一个文件,只需将
-lm
添加到命令行中,否则,请提供有关构建过程的一些信息。I suppose you have imported math.h with
#include <math.h>
So the only other reason I can see is a missing linking information. You must link your code with the
-lm
option.If you're simply trying to compile one file with gcc, just add
-lm
to your command line, otherwise, give some informations about your building process.这是我的观察,首先您需要将头文件
math.h
包含为math.h
头文件中声明的sqrt()
函数。例如,其次,如果您阅读 sqrt 的手册页,您会注意到这一行与-lm链接。
编译器错误是正确的,因为您尚未将程序与库
lm
& 链接起来。链接器无法找到sqrt()
的引用,您需要显式链接它。例如Here are my observation, firstly you need to include the header
math.h
assqrt()
function declared inmath.h
header file. For e.gsecondly, if you read manual page of sqrt you will notice this line Link with -lm.
Compiler error is correct as you haven't linked your program with library
lm
& linker is unable to find reference ofsqrt()
, you need to link it explicitly. For e.g只需在 c 源文件中添加
#include
并在 Makefile 最后添加 -lm 就可以了。Just adding the
#include <math.h>
in c source file and -lm in Makefile at the end will work for me.我遇到了同样的问题,但我只是通过在运行代码的命令后添加 -lm 来解决它。
例子。
gcc代码.c -lm
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