2015-05-26 77 views
1

編譯使用gcc-5.1.0下面的代碼生成一個警告:函數abs的隱式聲明 - GCC-5.1.0

warning: implicit declaration of function ‘abs’ [-Wimplicit-function-declaration] 

代碼:

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

int main (void) 
{ 
    printf ("%d\n", abs (-1)); 

    return 0; 
} 

我編了相同的代碼與gcc-4.9.2並沒有產生任何警告。

回答

1

abs()函數在<stdlib.h>中聲明,您沒有包含它。

GCC 4.9.2並沒有抱怨,因爲默認的編譯模式爲C89/C90(-std=gnu89)和功能並不需要按原樣返回int在C89在使用前,只要申報,但默認編譯模式在GCC 5.1.0中更改爲C11(-stdd=gnu11)(請參閱release notes),並且在使用C11函數之前必須聲明(或定義)C11函數。

1

嘗試在您的代碼中包含<stdlib.h>abs()函數定義在<stdlib.h>