未定义对"sqrt"的引用 [英] undefined reference to `sqrt'
问题描述
我程序的一部分是计算浮点数sqrt
.
编写sqrt(1.0f);
时成功编译程序,但是编写sqrt(-1.0f);
时成功
编译失败并显示undefined reference to 'sqrt'
-我想在这种情况下,将返回nan
值...
我用gcc编译程序.
当我用Visual Studio编译它时,它对sqrt的否定参数编译成功.
如何解决问题
谢谢
Part of my program is to calculate sqrt
of float number.
When I write sqrt(1.0f);
I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt'
- I suppose that in this case the nan
value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
推荐答案
您必须在大多数基于Unix的系统上添加-lm
标志,例如:
You have to add the -lm
flag on most Unix-based systems, as in:
编译使用:
gcc -c file.c
,然后使用以下链接:
gcc -o program file.o -lm
或者,如果您不想将两个编译步骤分开,只需编写:
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
这篇关于未定义对"sqrt"的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!