c - 在 C 中使用 math.h sqrt 函数

标签 c unix math sqrt

<分区>

阅读 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;
}

最佳答案

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

gcc you_file.c -o my_sqrt -lm

关于c - 在 C 中使用 math.h sqrt 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16576920/

相关文章:

c - 在c中堆栈Pop程序,每次都返回错误的输出

c - 使用 c 从文本文件中删除前导行

linux - 使用shell脚本调用目录下一些不规则命名的文件

unix - ksh 中的变量间接

c - 从文件中分割字符串然后存储到结构中

c - 以不同名称导入函数

c++ - C++ 中的统计和 S_IFREG

math - float 学运算是否被破坏?

c# - 仅使用数学函数的规则向上/向下舍入?

math - 无递归的浮点幂函数