在C中使用math.h sqrt函数 [英] Using math.h sqrt function in C

查看:170
本文介绍了在C中使用math.h sqrt函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读math.h的文档,似乎我所要做的只是包括math.h,并使用包含的数学函数,例如sqrt.问题是尝试在程序中使用sqrt时出现以下错误.我尝试了math.sqrt,但是那也不起作用.知道我在做什么错吗?

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

推荐答案

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

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

这篇关于在C中使用math.h sqrt函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆