我不知道我是否遗漏了一些明显的东西,但似乎我无法计算 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() 不应该与变量或其他东西一起使用吗?
谢谢您的帮助