首页 文章

在C [duplicate]中使用math.h sqrt函数

提问于
浏览
2

这个问题在这里已有答案:

阅读math.h的文档,看起来我应该做的就是包含math.h,并使用包含的数学函数,例如sqrt . 问题是我在程序中尝试使用sqrt时出现以下错误 . 我试过math.sqrt,但那也行不通 . 知道我做错了什么吗?

undefined reference to `sqrt'

...

#include <stdio.h>
#include <math.h>

int main (int argc, char *argv[])
{
  int a, b;

  a = 1;
  b = 5;

  if (a < sqrt (b))
    printf("1 < sqrt(5)");

  return 0;
}

1 回答

  • 4

    您需要明确地与数学库链接,因为 sqrt 依赖于它 . 将 -lm 附加到编译行重试:

    gcc you_file.c -o my_sqrt -lm
    

相关问题