419

The only way to learn a new programming language is by writing programs in it.

在linux用<math.h>的问题

,

http://www.linuxsir.org/bbs/showthread.php?t=242525

kikiwarm
资 料:
注册会员
注册日期: Dec 2005
帖子: 55
精华: 0

发帖时间: 06-02-13, 19:04 

在linux用<math.h>的问题 

我想用标准库里的 pow() 函数,写了个程序如下

#include <stdio.h>
#include <math.h>

int main()
{
int i;
i = pow(2,2);
printf("2x2 is %d", i);

return 0;
}

但是gcc报错,

/tmp/ccSb96nM.o(.text+0x24): In function `main':
: undefined reference to `pow'
collect2: ld returned 1 exit status

我已经inlucde了<math.h>,为什么会出现这样的错误?

MatthewGong

资 料:
注册会员
注册日期: Mar 2005
我的住址: 南京
帖子: 147
精华: 0

发帖时间: 06-02-13, 21:11 

编译时需要开发库的头文件, 连接时需要开发库的库文件。
math.h 对应的库是/lib/libm.so.6或/usr/lib/libm.so

这样: gcc main.c -lm
或:
gcc -c main.c
gcc -o main main.o -lm

不过通过使用/etc/ld.so.conf与/sbin/ldconfig对动态库进行缓存,好象可以略去-lm。
比如我运行你的例子用命令
[matt@localhost tmp]$ gcc main.c
[matt@localhost tmp]$ ./a.out
2x2 is 4[matt@localhost tmp]$

哪位可以具体谈谈其原理。
_____________________________________
学校:
升技KA7+Athon 1G+40G+256M+CDROM+FC3

家用:
P4P800+P4 HT 2.6G+希捷 SATA 120G x2+1024M+DVDROM+CDRW+FC4+WinXP

笔记本:
HP COMPAQ NX9040+FC4+WinXP
__________________________
来自南航,学习飞行力学.
喜欢自由软件

[url]http://jsbsim.sf.net[/url]
[url]http://www.flightgear.org[/url]
[url]http://www.wxwindows.org[/url]

The C Programming Language里的例程。search/C语言

Write a comment

New comments have been disabled for this post.