`
zhaoningbo
  • 浏览: 609704 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • c
阅读更多
引言:

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

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


正文:

  首先上段源代码:
#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语言新手学习群的群 ~。=


引用:

1)问题1解决方法






1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics