簡體   English   中英

為什么clang與gcc不同,需要-lm?

[英]Why does clang require -lm unlike gcc?

我遇到了一個奇怪的問題,我需要將-lm傳遞給clang以便編譯代碼:

gcc test.c -o test       #works
clang test.c -o test     #doesn't work
clang -lm test.c -o test #works


#include <stdio.h>
#include <complex.h>

int main() {
    double complex z = 1.0 + 3.0 * I;
    double complex conjugate = conj(z);
    printf("The conjugate of Z is = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));
    return 0;
}

具體來說,存在鏈接器錯誤:

/tmp/test-561678.o: In function `main':
test.c:(.text+0x4a): undefined reference to `conj'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我注意到的一件重要事情是,在這種情況下,gcc可以輕松勝過clang,因為gcc內聯了函數調用,而clang卻沒有:

鐺:

$ nm -g test
0000000000601048 B __bss_start
                 U conj@@GLIBC_2.2.5
...

gcc:

$ nm -g test
0000000000601038 B __bss_start
...

我使用kubuntu 16.04。 Clang 3.8版本和5.4.0 gcc版本。

有沒有辦法對這些函數進行clang內聯調用?

GCC提供了許多內置功能

6.59 GCC提供的其他內置功能

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

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

...

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,clogf,clogl,clog,conjf,conjl,conj,copysignf,copysignl,copysign,cpowf,cpowl,cpow,cprojf,cprojl,cproj,crealf,creall,creal,csinf,csinhf,csinhl,csinh, csqrtf,csqrtl,csqrt,ctanf,ctanhf,ctanhl,ctanh,ctanl,ctan,erfcf,erfcl,erfc,erff,erfl,erfcf,exp2f,exp2l,exp2,expm1f,expm1l,expm1,fdim,fdim, fmal,fmaxf,fmaxl,fmax,fma,fminf,fminl,fmin,hypotf,hypotl,hypot,ilogbf,ilogbl,ilogb,imaxabs,isblank,iswblank,lgammaf,lgammal,lgamma,llabs,llrintf,llrintl,llrint llroundl,llround,log1pf,log1pl,log1p,log2f,log2l,log2, logbf,logbl,logb,lrintf,lrintl,lrint,lroundf,lroundl,lround,neighborintf,neighborintl,neighborint,nextafterf,nextafterl,nextafter,nexttowardf,nexttowardl,nexttoward,remainderf,其余,剩余,remquof,remquol,remquo,r分別處理rintl,rint,roundf,roundl,round,scalblnf,scalblnl,scalbln,scalbnf,scalbnl,scalbn,snprintf,tgammaf,tgammal,tgamma,truncf,truncl,trunc,vfscanf,vscanf,vsnprintf和vsscanf函數除非在嚴格的ISO C90模式下(-ansi或-std = c90)。

...

由於GCC提供conj()作為內置函數,因此在使用GCC進行編譯時,無需使用-lm選項鏈接libm.so (或libm.a )。

暫無
暫無

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

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