Using math.h sqrt function in C [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-11 01:22:27

问题


Reading the documentation for math.h, it seems like all I should have to do is include math.h, and use the math functions included, such as sqrt. The problem is I get the following error when trying to use sqrt in my program. I tried math.sqrt, but that did not work, either. Any idea what I am doing wrong?

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:


You need to explicitely link with the math library as sqrt depends on it. Retry with appending -lm to your compilation line :

gcc you_file.c -o my_sqrt -lm


来源:https://stackoverflow.com/questions/16576920/using-math-h-sqrt-function-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!