2013-05-16 48 views
2

阅读math.h的文档,似乎我应该做的就是包含math.h,并使用包含的数学函数,如sqrt。问题是我试图在我的程序中使用sqrt时出现以下错误。我试过math.sqrt,但那也没用。任何想法我做错了什么?在C中使用math.h 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; 
} 
+2

也许用'-lm'选项 – BLUEPIXY

+0

我知道它说'sin',但它是任何数学函数相同。 – hammar

回答

4

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

gcc you_file.c -o my_sqrt -lm 
+0

-lm工作。谢谢! – user2227422

+0

为什么这个图书馆需要在其他人不需要时明确链接? – LazerSharks

+0

没关系,找到答案:http://stackoverflow.com/questions/4606301/gcc-why-the-lm-flag-is-needed-to-link-the-math-library – LazerSharks