gcc

来源:互联网 发布:王力宏 李靓蕾 知乎 编辑:程序博客网 时间:2024/06/05 05:11
    #include<stdio.h>  
    #include<math.h>  
      
    int main()  
    {  
            double d=log(8.0);  
            double d1=exp(2.0);  
            printf("d=%lf d1=%lf\n",d,d1);  
            return 0;  
    }  


以上源代码在编译时(源代码保存在test.c源文件中)
[html] view plaincopy


    gcc -c  test.c  


不会出现错误。但是在连接生成可执行文件时
[html] view plaincopy


    gcc -o test test.o  


编译器会提如下错误信息
[html] view plaincopy


    test.c:(.text+0x23): undefined reference to `log'  
    test.c:(.text+0x48): undefined reference to `exp'  
    collect2: ld returned 1 exit status  


原因是:gcc编译器要求在使用某些数学函数时,需要在链接的时候加-lm选项。即若以上编译、链接过程如下:
[html] view plaincopy


    gcc -c test.c  
    gcc -o test test.o -lm  


则不会报错,并且生成正确的可执行程序。或者编译、链接合在一起:
[html] view plaincopy


    gcc test.c -lm  


这样也不会出错,并生成名字为a.out的可执行文件。
原创粉丝点击