一个关于简单函数调用的问题,百思不得其解

betterzy 2006-03-31 11:17:34
#include <stdio.h>
#include <math.h>

int main(void)
{
float pi ;
float pisqrt;

pi = 3.14;
pisqrt = sqrtf(3.14 );//<-----------这里不同

printf("pi=%f\n",pi);
printf("pisqrt = %f\n",pisqrt);

return 0;
}
--------------------------------------
结果正常:
[root@zhangyong develop]# make
gcc -c study.c -o study.o
gcc study.o -o study
[root@zhangyong develop]# ./study
pi=3.140000
pisqrt = 1.772004
[root@zhangyong develop]#
-------------------------------------
而改成变量,就不对了
#include <stdio.h>
#include <math.h>

int main(void)
{
float pi ;
float pisqrt;

pi = 3.14;
pisqrt = sqrtf(pi);//<-----------这里不同

printf("pi=%f\n",pi);
printf("pisqrt = %f\n",pisqrt);

return 0;
}
------------------------------------
结果:
[root@zhangyong develop]# make
gcc -c study.c -o study.o
gcc study.o -o study
study.o(.text+0x2b): In function `main':
: undefined reference to `sqrtf'
collect2: ld returned 1 exit status
make: *** [study] 错误 1
[root@zhangyong develop]#
...全文
138 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
betterzy 2006-03-31
  • 打赏
  • 举报
回复
我加了-lm 还是不能通过,我装的是
[root@zhangyong ~]# uname -a
Linux zhangyong 2.6.9-11.EL #1 Fri May 20 18:17:57 EDT 2005 i686 i686 i386 GNU/Linux
[root@zhangyong ~]# gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.3 20050227 (Red Hat 3.4.3-22.1)
[root@zhangyong ~]# make -v
GNU Make 3.80
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
[root@zhangyong ~]#
YanDong_8212 2006-03-31
  • 打赏
  • 举报
回复
你用的什么系统,我不加-lm 也能通过。
joyself 2006-03-31
  • 打赏
  • 举报
回复
That's right

math的库
vanlover 2006-03-31
  • 打赏
  • 举报
回复
楼上正确
alaiyeshi 2006-03-31
  • 打赏
  • 举报
回复
gcc -lm study.o -o study
就可以了
lilywon 2006-03-31
  • 打赏
  • 举报
回复
gcc pi.c -lm -o pi 刚刚试过了,是好的。
alaiyeshi 2006-03-31
  • 打赏
  • 举报
回复
-lm
是使用的libmath.so这个动态库
betterzy 2006-03-31
  • 打赏
  • 举报
回复
不好意思噢,好像加了 -lm 可以了,上午好像试过不行的,唉,可能是上课命令打错了

不过,要问问 -lm,是作什么用的,刚学LINUX编程,望勿见怪!
betterzy 2006-03-31
  • 打赏
  • 举报
回复
sqrt,一样不行
YanDong_8212 2006-03-31
  • 打赏
  • 举报
回复
在math.h里面我找不到sqrtf,不知道是不是版本引起的,但是如果我用g++编译就不存在这个问题,而且用g++编译根本不需要math.h头文件,说明sqrtf由C++自动支持的。如果实在要用gcc编译,建议你用sqrt
alaiyeshi 2006-03-31
  • 打赏
  • 举报
回复
把你加了-lm以后报的错贴出来看看

23,115

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧