gcc

    #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的可执行文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值