【发布时间】:2011-04-01 19:22:17
【问题描述】:
我不知道我是否遗漏了一些明显的东西,但似乎我无法在 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() 不应该与变量或其他东西一起使用吗?
感谢您的帮助
【问题讨论】:
标签: c compiler-errors math.h sqrt