sqrt() 函数不适用于变量参数
我不知道我是否遗漏了一些明显的东西,但似乎我无法计算 C 中变量的平方根; sqrt() 函数似乎只适用于常量。这是我的代码:
#include <math.h>
#include <stdio.h>
int main()
{
double a = 2.0;
double b = sqrt(a);
printf("%f", b);
return 0;
}
当我运行此程序时,出现以下错误:
gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler)
/tmp/ccVfxkNh.o: In function `main':
test2.c:(.text+0x30): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Compilation failed.
但是,如果我将 sqrt() 中的参数替换为常量,例如 2.0,(b = sqrt(2.0)
),然后就可以正常工作了。 sqrt() 不应该与变量或其他东西一起使用吗?
感谢您的帮助
I don't know if I'm missing something obvious, but it appears that I'm unable to compute square roots of a variable in C; the sqrt() function only seems to work on constants. This is my code:
#include <math.h>
#include <stdio.h>
int main()
{
double a = 2.0;
double b = sqrt(a);
printf("%f", b);
return 0;
}
When I run this program, I get the following error:
gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler)
/tmp/ccVfxkNh.o: In function `main':
test2.c:(.text+0x30): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Compilation failed.
However, if I replace the argument in sqrt() with a constant such as 2.0 for example, (b = sqrt(2.0)
), then it works fine. Is sqrt() not supposed to work with variables or something?
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要链接数学库(在命令行上使用“-lm”)。在恒定情况下,编译器可能很聪明并预计算 sqrt(2.0) (因此编译的代码本质上是“b = 1.414...;”)
You need to link with the math library (use a '-lm' on the command line). In the constant case, the compiler is probably being smart and precomputing sqrt(2.0) (so the code that is compiled is essentially 'b = 1.414...;')
如果是 gcc,您需要链接库。
gcc 文件名.c -lm 。
但是,如果是 g++,则无需链接库,因此可以正常工作:
g++ filename.c -o filename
一旦编译成功。
要运行,只需在 G++ 中输入 ./filename 即可。
并在 Gcc 中输入 ./a.out。
In case of gcc you need to link the library.
gcc filename.c -lm .
However in case of g++ no need to link the library so this will work fine :
g++ filename.c -o filename
Once compilation is successful.
To run simply enter ./filename in G++.
and enter ./a.out in Gcc.
使用命令
gcc -Wall -o "test2" "test2.c" -lm
可能会解决此问题。除了标准 C 运行时库之外,这还包括数学库。在大多数系统上,数学库历来是一个需要明确请求的独立实体。
Use the command
gcc -Wall -o "test2" "test2.c" -lm
which will likely fix this.This includes the math library in addition to the standard C runtime library. On most systems, the math library is historically a separate entity that needs to be explicitly requested.
编译:
您需要链接数学库。
Compile with:
You need to link against the math library.
使用“”运算符包含数学库
使用
-lm
选项编译程序以继承数学库假设我们的程序名称是
test.c
我们编译如下include math library using " " operator
compile program using
-lm
option for inherit math librarysuppose our program name is
test.c
the we compile as follow默认情况下,gcc 不链接标准库。因此,如果通过 gcc 编译,您只需要执行此操作:
gcc filename.c -lm 。
但是,如果是 g++,则无需链接库,因此可以正常工作:
g++ filename.c -o filename
gcc does not link the standard libraries by default. So you just need to do this if compiling via gcc:
gcc filename.c -lm .
However in case of g++ no need to link the library so this will work fine :
g++ filename.c -o filename
这对我来说效果很好。我认为你的数学库有问题。尝试再次链接并查看。其他方面的代码是完全完美的。
This works fine for me. I think there is some problem with ur math library. Try linking it again and see. Other wise code is completely perfect.