2017-08-03 5 views
1

私はそれがコードをコンパイルするために打ち鳴らすために-lmを渡す必要がある奇妙な問題に遭遇しました:なぜclangはgccと違って-lmを必要としますか?

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で打ち鳴らすにはないのに対し、gccのインライン通話機能するので、簡単に打ち鳴らすをアウトパフォームすることが可能であることである:

打ち鳴らす:

$ nm -g test 
0000000000601048 B __bss_start 
       U [email protected]@GLIBC_2.2.5 
... 

GCC:

$ nm -g test 
0000000000601038 B __bss_start 
... 

私はKubuntuの16.04を使用しています。 Clang 3.8バージョン、および5.4.0 gccバージョンです。

これらの機能をclangインラインで呼び出す方法はありますか?

+0

でコンパイル異なるコンパイラであり、ライブラリの実装が異なりますか? –

+0

@AjayBrahmakshatriyaこれは図書館の問題ではありません。それはGCC拡張です - GCCは多くの組み込み関数を提供します。 –

+0

@AndrewHenle by library私は実際のlib(またはそれ)のファイルを意味しませんでした。私は、ライブラリが複素数を使用することを意味しました。 1つは組み込み関数を使用して実装し、もう1つは関数呼び出しを使用して実装します。私は間違った言葉を使った。 –

答えて

4

GCC provides numerous built-in functions

6.59 Other Built-in Functions Provided by GCC

GCC provides a large number of built-in functions other than the ones mentioned above. Some of these are for internal use in the processing of exceptions or variable-length argument lists and are not documented here because they may change from time to time; we do not recommend general use of these functions.

The remaining functions are provided for optimization purposes.

...

The ISO C99 functions _Exit, acoshf, acoshl, acosh, asinhf, asinhl, asinh, atanhf, atanhl, atanh, cabsf, cabsl, cabs, cacosf, cacoshf, cacoshl, cacosh, cacosl, cacos, cargf, cargl, carg, casinf, casinhf, casinhl, casinh, casinl, casin, catanf, catanhf, catanhl, catanh, catanl, catan, cbrtf, cbrtl, cbrt, ccosf, ccoshf, ccoshl, ccosh, ccosl, ccos, cexpf, cexpl, cexp, cimagf, cimagl, cimag, clogf, clogl, clog, conjf, conjl, conj, copysignf, copysignl, copysign, cpowf, cpowl, cpow, cprojf, cprojl, cproj, crealf, creall, creal, csinf, csinhf, csinhl, csinh, csinl, csin, csqrtf, csqrtl, csqrt, ctanf, ctanhf, ctanhl, ctanh, ctanl, ctan, erfcf, erfcl, erfc, erff, erfl, erf, exp2f, exp2l, exp2, expm1f, expm1l, expm1, fdimf, fdiml, fdim, fmaf, fmal, fmaxf, fmaxl, fmax, fma, fminf, fminl, fmin, hypotf, hypotl, hypot, ilogbf, ilogbl, ilogb, imaxabs, isblank, iswblank, lgammaf, lgammal, lgamma, llabs, llrintf, llrintl, llrint, llroundf, llroundl, llround, log1pf, log1pl, log1p, log2f, log2l, log2, logbf, logbl, logb, lrintf, lrintl, lrint, lroundf, lroundl, lround, nearbyintf, nearbyintl, nearbyint, nextafterf, nextafterl, nextafter, nexttowardf, nexttowardl, nexttoward, remainderf, remainderl, remainder, remquof, remquol, remquo, rintf, rintl, rint, roundf, roundl, round, scalblnf, scalblnl, scalbln, scalbnf, scalbnl, scalbn, snprintf, tgammaf, tgammal, tgamma, truncf, truncl, trunc, vfscanf, vscanf, vsnprintf and vsscanf are handled as built-in functions except in strict ISO C90 mode (-ansi or -std=c90).

...

GCCは組み込み関数としてconj()を提供するので、あなたがlibm.so(またはlibm.a)にリンクする必要はありません-lmオプションを使用して、彼らのでGCC

+0

これらの関数をインライン化する方法を知っていますか?私は集中的にclangを使用しています。私の場合、gccはこれらの機能を呼び出すため、多くの要因によってclangより優れています。 – Goovie

+0

@Goovie clang最適化に関する情報については、https://stackoverflow.com/questions/15548023/clang-optimization-levelsを参照してください。 –

+0

私はこのテストを修正し、どちらのコンパイラでも "-O3 -flto"で再コンパイルしましたが、clangでこれらの関数をインライン化することはできません。clangにはこれらの関数がインライン化することはできません明らかにこれらの関数をインライン化したいのであれば、私はそれらを自分で書く必要があります。 – Goovie

関連する問題