2013-05-16 51 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