代码之家  ›  专栏  ›  技术社区  ›  Goovie

为什么clang需要-lm而不是gcc?

  •  1
  • Goovie  · 技术社区  · 7 年前

    我遇到了一个奇怪的问题,我需要将-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)
    

    叮当声:

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

    通用条款:

    $ nm -g test
    0000000000601038 B __bss_start
    ...
    

    有没有办法对这些函数进行叮当的内联调用?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Community CDub    4 年前

    GCC provides numerous built-in functions

    6.59 GCC提供的其他内置功能

    异常或可变长度参数列表,并且没有记录 这是因为它们可能会不时变化;我们不建议

    asinh,atanhf,atanhl,atanh,cabsf,cabsl,cabs,cacosf,cacosf, cacoshl,cacosh,cacosl,cacos,cargf,cargl,carg,casinf,casinhf, catanl,catan,cbrtf,cbrtl,cbrt,ccosf,ccoshf,ccoshl,ccosh, ccosl,ccos,cexpf,cexpl,cexp,cimagf,cimagl,cimag,clogf,clogl, 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, fmaf、fmal、fmaxf、fmaxl、fmax、fma、fminf、fminl、fmin、HYPERTF、, lgammaf,lgammal,lgamma,llabs,llrintf,llrintl,llrint,LLRONDF, llroundl,llround,log1pf,log1pl,log1p,log2f,log2l,log2,logbf, logbl,logb,lrintf,lrintl,lrint,lroundf,lroundl,lround, 下一个,下一个,下一个,剩余F,剩余L, 余数,remquof,remquol,remquo,rintf,rintl,rint,roundf, roundl,round,scalblnf,scalblnl,scalbln,scalbnf,scalbnl,scalbnl,scalbn, snprintf,tgammaf,tgammal,tgamma,truncf,truncl,trunc,vfscanf, vscanf、vsnprintf和vsscanf作为内置函数处理,除了

    ...

    由于GCC提供 conj() libm.so (或 libm.a -lm 使用GCC编译时的选项