簡體   English   中英

函數fmax的隱式聲明

[英]Implicit declaration of function fmax

我有以下代碼:

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

int main(void) {
    printf("%f\n", fmax(1.2, 3.4));
    return 0;
}

如果我編譯:

gcc a.c -o a && ./a

然后我得到預期的輸出:

3.400000

如果我嘗試啟用警告並以C89為目標,則無法編譯它:

$ gcc -Wall -Wextra -std=c89 -pedantic -Wstrict-prototypes a.c -o a
a.c: In function ‘main’:
a.c:5:5: warning: implicit declaration of function ‘fmax’ [-Wimplicit-function-declaration]
a.c:5:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]
/tmp/cc8d2iQl.o: In function `main':
a.c:(.text+0x1d): undefined reference to `fmax'
collect2: ld returned 1 exit status
$ gcc -Wall -Wextra -std=c89 -pedantic -Wstrict-prototypes a.c -lm -o a
a.c: In function ‘main’:
a.c:5:5: warning: implicit declaration of function ‘fmax’ [-Wimplicit-function-declaration]
a.c:5:5: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]

我發現fmax()僅由C99標准定義,而不是C89。 因此,問題是:為什么這些完全相同的命令在Mac上卻不發出任何警告而在Linux機器上卻沒有發出警告,卻能正常工作?

我想你需要建立與-std = C99(見fmax的手冊頁)..看到

fmaxf手冊頁

fmax(), fmaxf(), fmaxl():
_XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L;
or cc -std=c99

看來fmax也需要C99

從gcc 文檔

5.44 GCC提供的其他內置功能

除了上述功能外,GCC還提供了大量內置功能。 其中一些是在內部處理異常或變長參數列表時使用的,此處將不作記錄,因為它們可能會不時更改; 我們不建議您一般使用這些功能。

提供其余功能是出於優化目的。

GCC在標准C庫中包括許多功能的內置版本。 即使指定了-fno-builtin選項,帶_ 內置前綴的版本也將始終被視為與C庫函數具有相同的含義。 (請參閱C語言選項)這些功能中的許多功能僅在某些情況下才進行了優化; 如果在特定情況下未對其進行優化,則會發出對庫函數的調用。

在嚴格的ISO C模式下(-ansi,-std = c89或-std = c99),函數_exit,alloca,bcmp,bzero,dcgettext,dgettext,dremf,dreml,drem,exp10f,exp10l,exp10,ffsll,ffsl, ffs,fprintf_unlocked,fputs_unlocked,gammaf,gammal,gamma,gettext,index,isascii,j0f,j0l,j0,j1f,j1l,j1,jnf,jnl,jn,mempcpy,pow10f,pow10l,pow10,rff,printfun scalbl,scalb,signbit,signbitf,signbitl,significandf,significandl,significand,sincosf,sincosl,sincos,stpcpy,strdup,strfmon,toascii,y0f,y0l,y0,y1f,y1l,y1,ynf,ynl和作為內置功能。 所有這些功能都有對應的版本,它們以_ builtin開頭 ,即使在嚴格的C89模式下也可以使用。

ISO C99功能_Exit,acoshf,acoshl,acosh,asinhf,asinhl,asinh,atanhf,atanhl,atanh,cabsf,cabsl,cabs,cacosf,cacoshf,cacoshl,cacosh,cacosl,cacosf,cargf,cargl,carg,casin casinhf,casinhl,casinh,casinl,casin,catanf,catanhf,catanhl,catanh,catanl,catan,cbrtf,cbrtl,cbrt,ccosf,ccoshf,ccoshl,ccosh,ccosl,ccosl,cexpf,cexpl,cexp,cexp,cexp cimag,conjf,conjl,conj,copysignf,copysignl,copysign,cpowf,cpowl,cpow,cprojf,cprojl,cproj,crealf,creall,creal,csinf,csinhf,csinhl,csinh,csinl,csin,cqsf ctanf,ctanhf,ctanhl,ctanh,ctanl,ctan,erfcf,erfcl,erfc,erff,erfl,erf,exp2f,exp2l,exp2,expm1f,expm1l,expm1,fdimf,fdiml,fdim,fmax, fmaf ,fmal,fmal fmax ,fma,fminf,fminl,fmin,hypotf,hypotl,hypot,ilogbf,ilogbl,ilogb,imaxabs,isblank,iswblank,lgammaf,lgammal,lgamma,llabs,llrintf,llrintl,llrint,llroundf,llroundl, log1pl,log1p,log2f,log2l,log2,logbf,logbl,logb ,lrintf,lrintl,lrint,lroundf,lroundl,lround,neighborintf,neighborintl,neighborint,nextafterf,nextafterl,nextafter,nexttowardf,nexttowardl,nexttoward,remainderf,remainderl,remainder,remquof,remquol,remquo,rintf,roundder,roundrin,rintf,rintf,rintf ,roundl,round,scalblnf,scalblnl,scalbln,scalbnf,scalbnl,scalbn,snprintf,tgammaf,tgammal,tgamma,truncf,truncl,trunc,vfscanf,vscanf,vsnprintf和vsscanf在嚴格的ISO C中被處理為內置函數模式(-ansi或-std = c89)。

它是由C99而不是C89定義的。

man中搜索“ C99”以獲取fmaxf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM