2014-11-17 68 views
-3

所以我試圖做一個程序來計算二次公式,但是當我嘗試編譯代碼時,我得到以下:「未定義的對sqrt的引用」 但我試着通過數學來定義sqrt .h和其他2個代碼。 我重視我的代碼 任何幫助,將不勝感激Sqrt功能不工作

#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 

double sqrt(double); 

int main (void) { 
    double sqrt(double); 
    int a,b,c; 
    double discriminant,squarerootofdis,root1, root2; 
    printf("Please enter the coefficient of x^2:"); 
    scanf("%d",&a); 
    printf("Please enter the coefficient of x:"); 
    scanf("%d",&b); 
    printf("Please enter the integer value of the ploynomial:"); 
    scanf("%d",&c); 
    if (a==0 && b==0) 
    {printf("This case is extremely degenerate");} 
    else if (a==0 && b!=0) 
    {root1=-c/b; 
     printf("Degenerate  one real root: %lf\n",root1);} 
    else{ 
    discriminant = ((b*b)-(4*a*c)); 
    squarerootofdis = sqrt(discriminant); 
    root1 = (squarerootofdis-b)/(2*a); 
    root2 = (-squarerootofdis-b)/(2*a); 
    if (discriminant>0) 
     printf("Two real roots: %lf\n %lf\n", root1, root2); 
    else if (discriminant == 0) 
     printf("Degenerate  one real root: %lf\n",root1); 
    else if (discriminant<0) 
     printf("Two complex roots: %lf\n %lf\n", root1, root2); 
    } 
} 
+1

這與Emacs有什麼關係? –

+0

我在emacs的代碼 – Mike

+0

所以沒有,我刪除'emacs'標籤 –

回答

1

你與-lm鏈接編譯?

頭文件將提供decarrationsqrt()函數。要使的定義爲,您需要鏈接到由函數定義組成的庫math

例子:

gcc test.c -o output -lm 
2

要使用sqrt功能(或math.h定義的任何功能),你必須在m庫鏈接:

~$ gcc -lm yourcode.c -o program 
+0

是-onem還是-letterlm或-captialim? – Mike

+0

這是'gcc'的'-l'(即小寫L)選項(讀取聯機幫助頁),參數爲'm'。這將使'gcc'動態地與你的程序鏈接'libm'庫。檢查此頁面:http://en.wikipedia.org/wiki/Dynamic_linker –

0

請使用以下命令

gcc test.c -lmath