我对C非常陌生,我有以下代码:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.5;
double result = sqrt(x);
printf("The square root of %lf is %lf\n", x, result);
return 0;
}
但当我用以下方法编译时:
gcc test.c -o test
我会遇到这样的错误:
/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
为什么会这样?math.h
头文件中不是sqrt()
吗?对于cosh
和其他三角函数,我得到了同样的误差.为什么?