调试程序出现的错误,本来以为在文件中引用"math.h"就不会有问题,结果发现这个错误原来还是挺普遍的,下面是我在网上搜的一个解决贴 作者:smily2005 我的系统是ubuntu-8.10-desktop-amd64,我想用函数sqrt(),但是编译出错.代码如下: #include <stdio.h> #include <math.h> int main() { float fn, fres; double dn, dres; printf("Enter a float number: " ); scanf("%f", &fn); fres = sqrtf(fn); printf("The result of sqrt(%f) = %f\n", fn, fres ); printf("Enter a double number: "; scanf("%lf", &dn); dres = sqrt(dn); printf("The result of sqrt(%lf) = %lf\n", dn, dres ); printf("The result of sqrt(2)=%f\n", sqrt(2) ); } 编译错误: In function 'main': trysqrt.c.text+0x51): undefined reference to 'sqrtf' trysqrt.c.text+0xcc): undefined reference to 'sqrt' 我查看了/usr/include/math.h头文件,但是找不到sqrt()函数声明,请问这是怎么回事? 作者: xiaoshao_0_0 作者:smily2005 谢谢! 问题解决. 但是我还是不明白. 引用:
作者:windwhinny 首先要对编译有个了解,你写了一个程序,如果有头文件的话,就需要编译器指定这头文件对应的库文件,库文件一般都在/usr/lib目录下。 gcc默认指定的有几个库文件,比如libstd。 但是你所需要的math库不是gcc默认指定的,所以就需要你在编译的时候加上一个-lm选项。 -l是指定XXX库,m就指math库。 |
|
来自: XeonGate > 《Linux编程接口》