相关疑难解决方法(0)

为什么即使我包含math.h标题,我也会收到"对sqrt的未定义引用"错误?

我是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;
}
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时:

gcc test.c -o test
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误:

/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

为什么会这样?是sqrt()不是在math.h头文件中?我cosh和其他三角函数有同样的错误.为什么?

c linker linker-errors libm

98
推荐指数
5
解决办法
19万
查看次数

标签 统计

c ×1

libm ×1

linker ×1

linker-errors ×1