LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1426|回复: 6

关于函数中引用系统函数的问题

[复制链接]
发表于 2008-10-31 18:45:05 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. #include <math.h>
  3. int loop(int x)
  4. {
  5.         int y = 0;
  6.         for (; x != 0; x /= 10)
  7.                 y = y * 10 + x % 10;
  8.         return y;
  9. }
  10.                                                                                                                                                
  11. int prime(int x)
  12. {
  13.         int i, sqx;
  14.         sqx = sqrt(x);
  15.         for (i = 1; i <= sqx; i++)
  16.                 if (x % i == 0)
  17.                         break;
  18.         if (i >= sqx)
  19.                 return 1;
  20.         else
  21.                 return 0;
  22. }
  23.                                                                                                                                                
  24.                                                                                                                                                
  25. int main()
  26. {
  27.         int i, n;
  28.         scanf ("%d", &n);
  29.         if (prime(n) == 1 && prime(loop(n)) == 1)
  30.                 printf ("yes");
  31.         else
  32.                 printf ("no");
  33.         return 0;
  34. }
复制代码

                                                                                                                                               
这个是一个验证可逆素数的程序,我的问题是,在VC下编译通过,但在linux下用gcc编译,会出现如下信息:

  1. /tmp/ccgom8l8.o(.text+0x6e): In function `prime':
  2. : undefined reference to `sqrt'
  3. collect2: ld returned 1 exit status
复制代码


请问其中原因?谢谢.
发表于 2008-10-31 19:41:19 | 显示全部楼层
需要数学库libm.so支持,编译链接时需要加开关 -lm
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-10-31 19:55:30 | 显示全部楼层
gcc -o -lm test getprimenum.c


...以下是错误信息:


  1. 1(.rodata+0x0): multiple definition of `_fp_hw'
  2. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.rodata+0x0):../sysdeps/i386/elf/start.S:47: first defined here
  3. 1(.data+0x4): In function `__data_start':
  4. : multiple definition of `__dso_handle'
  5. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/crtbegin.o(.data+0x0): first defined here
  6. 1(.init+0x0): In function `_init':
  7. /usr/src/build/231499-i386/BUILD/glibc-2.3.2-20030313/build-i386-linux/csu/crti.S:35: multiple definition of `_init'
  8. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crti.o(.init+0x0):/usr/src/build/231499-i386/BUILD/glibc-2.3.2-20030313/build-i386-linux/csu/crti.S:12: first
  9. defined here
  10. 1(.text+0x0): In function `_start':
  11. ../sysdeps/i386/elf/start.S:47: multiple definition of `_start'
  12. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x0):../sysdeps/i386/elf/start.S:47: first defined here
  13. 1(.fini+0x0): In function `_fini':
  14. /usr/src/build/231499-i386/BUILD/glibc-2.3.2-20030313/build-i386-linux/csu/crti.S:51: multiple definition of `_fini'
  15. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crti.o(.fini+0x0): first defined here
  16. 1(.got+0x0): multiple definition of `_GLOBAL_OFFSET_TABLE_'
  17. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.got.plt+0x0):../sysdeps/i386/elf/start.S:47: first defined here
  18. 1(.rodata+0x4): multiple definition of `_IO_stdin_used'
  19. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.rodata+0x4):../sysdeps/i386/elf/start.S:53: first defined here
  20. 1(.data+0x0): In function `__data_start':
  21. : multiple definition of `__data_start'
  22. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.data+0x0):../sysdeps/i386/elf/start.S:47: first defined here
  23. /tmp/ccCI3BAA.o(.text+0xcf): In function `main':
  24. : multiple definition of `main'
  25. 1(.text+0xb0): first defined here
  26. /usr/bin/ld: Warning: size of symbol `main' changed from 312 to 124 in /tmp/ccCI3BAA.o
  27. /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.dynamic+0x0):../sysdeps/i386/elf/start.S:47: multiple definition of `_DYNAMIC'
  28. 1(.dynamic+0x0): first defined here
  29. /tmp/ccCI3BAA.o(.text+0x6e): In function `prime':
  30. : undefined reference to `sqrt'
  31. collect2: ld returned 1 exit status

复制代码
回复 支持 反对

使用道具 举报

发表于 2008-10-31 21:39:56 | 显示全部楼层
楼主想一想 gcc 命令中的 -o 是什么意思?
回复 支持 反对

使用道具 举报

发表于 2008-10-31 22:58:47 | 显示全部楼层
-lm 必须放在最后面
回复 支持 反对

使用道具 举报

发表于 2008-11-1 11:41:11 | 显示全部楼层
-lm 不见得非要放在最后, 不过放在最后一般保证不出错就是了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-11-3 21:08:08 | 显示全部楼层
放到最后就没问题了,谢谢.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表