gcc编译包含数学函数的C代码时遇到的异常及解决

[b]引言:[/b]

最近对C语言来了兴趣,业余了解体验了一下。在使用gcc编译时遇到两个异常很有意思,留下此篇希望能帮到和我一样的新手们。

异常信息是:
1)undefined reference to `sin'
2)/usr/bin/ld: skipping incompatible /usr/lib/libc.so when searching for -lc


[b]正文:[/b]

首先上段源代码:
#include <stdio.h>
#include <math.h>

#define INT_COUNT 20

double f(int x);

int main(int argc, char *argv[]){
int i;
double results[INT_COUNT];

results[0] = f(1);
printf("%e\n", results[0]);

return 0;
}

double f(int x){
double result = 0.0, d;

d = 1.0 * x;
result = d - sin(d) - cos(d);

return result;
}


问题1:
[zhaoningbo@officetest1 CLang]$ gcc maxValue.c -o maxValue.out
/tmp/cc0QRY70.o: In function `f':
maxValue.c:(.text+0x48): undefined reference to `sin'
maxValue.c:(.text+0x70): undefined reference to `cos'
collect2: ld 返回 1
[zhaoningbo@officetest1 CLang]$

这个问题网上的资料比较多,一查便可知道“这种错误一般是由于缺少库造成的.使用-lm即可”。的确到此问题可以解决了。

问题2:
笔者在自己虚拟机解决完问题1后,编译成功了。却在测试服务器上,报了如下信息:
[zhaoningbo@officetest1 CLang]$ gcc maxValue.c -lm -L/lib -L/usr/lib -o maxValue.out
/usr/bin/ld: skipping incompatible /usr/lib/libm.so when searching for -lm
/usr/bin/ld: skipping incompatible /usr/lib/libm.a when searching for -lm
/usr/bin/ld: skipping incompatible /usr/lib/libc.so when searching for -lc
/usr/bin/ld: skipping incompatible /usr/lib/libc.a when searching for -lc
[zhaoningbo@officetest1 CLang]$

这是怎么回事呢?虚拟机用的32位系统,测试服务器是64位,于是一搜就有解决方法了。具体如下:

[zhaoningbo@officetest1 CLang]$ gcc maxValue.c -lm -L/lib64 -L/usr/lib64 -o maxValue.out
[zhaoningbo@officetest1 CLang]$ ./maxValue.out
-3.817733e-01
[zhaoningbo@officetest1 CLang]$

至此问题解决了。

最后推荐一个新手群(当然里面有高手)186660469。这是我找的最像C语言新手学习群的群 ~。=


[b]引用:[/b]

1)[url=http://hi.baidu.com/jangill/blog/item/accb4a0825ad883ce82488e2.html]问题1解决方法[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值