Linux系统怎么编译sin,linux下gcc编译sin函数出错的问题

linux下gcc编译sin函数出错的问题 收藏

Q: I keep getting errors due to library functions being undefined, but I'm #including all the right header files.

--------------------------------------------------------------------------------

A: In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the external library itself. The declarations in the header file only tell the compiler how to call the external functions; the header file doesn't supply the definitions of the external functions, or tell the compiler/linker where to find those definitions.

In some cases (especially if the functions are nonstandard) obtaining those definitions may require explicitly asking for the correct libraries to be searched when you link the program. (Some systems may be able to arrange that whenever you #include a header, its associated library, if nonstandard, is automatically requested at link time, but such a facility is not widespread.) See also questions 10.11 , 11.30 , 13.26 , 14.3 , and 19.40 .

Q: I'm trying to do some simple trig, and I am #including , but the linker keeps complaining that functions like sin and cos are undefined.

--------------------------------------------------------------------------------

A: Make sure you're actually linking with the math library. For instance, due to a longstanding bug in Unix and Linux systems, you usually need to use an explicit -lm flag, at the end of the command line, when compiling/linking. See also questions 13.25 , 13.26 , and 14.2 .

当使用gcc编译器编译含数学函数的C程序时,会出现undefined reference to `sin'错误.这种错误一般是由于缺少库造成的.使用-lm即可.Makefile可以这样写:

pe14-18-11:pe14-18-11.o

gcc pe14-18-11.o -lm -o pe14-18-11

pe14-18-11.o:pe14-18-11.c

gcc -c pe14-18-11.c -o pe14-18-11.o

clean:

rm -f *.o pe14-18-11

具体原因及解决办法为:

加入连结的函式库

刚刚我们都仅只是在屏幕上面印出一些字眼而已,如果说要计算数学公式呢?!例如我们想要计算出三角函数里面的 sin(90度角),要注意的是,大多数的程序语言都是使用径度而不是一般我们在计算的『角度』, 180 度角约等于 3.14 径度!嗯!那我们就来写一下这个程序吧!

[guest@test guest]# vi sin.c

#include

int main(void)

{

float value;

value = sin ( 3.14 / 2 );

printf("%f\n",value);

}

# 上面这个档案的内容可以在底下取得!

# http://linux.vbird.org/download/books/basic/source_code/sin.c

那要如何编译这支程序呢?我们先直接编译看看:

[guest@test guest]# gcc sin.c

/tmp/ccppUCx8.o(.text+0x1e): In function `main':

: undefined reference to `sin'

collect2: ld returned 1 exit status

特别注意上面的说明,唉啊!怎么没有编译成功?它说的是『 undefined reference to sin 』,说的是『 没有 sin 的相关定义参考值! 』,为什么会这样呢?这是因为 C 语言里面的 sin 函示是写在 libm.so 这个函式库中,而我们并没有在原始码里面加入相关的说明,所以当然就需要在编译与连结的时候将这个函式库给他连结进执行档里面啊!所以我们可以这样做:

[guest@test guest]# gcc sin.c -lm -L/lib -L/usr/lib

# 特别注意,那个 -lm 可以拆开成两部份来看,

# -l 是『加入某个函式库(library)』的意思,而

# m 则是 libm.so 这个函式库,其中, lib 与附档名(.a 或 .so)不需要写

# 所以 -lm 表示使用 libm.so (或 libm.a) 这个函式库的意思~

# 至于那个 -L 后面接的路径呢?这表示:

#『我要的函式库 libm.so 请到 /lib 或 /usr/lib 里面搜寻! 』

[guest@test guest]# ./a.out

1.000000

上面的说明很清楚了吧!!不过,要注意的是,由于 Linux 预设是将函式库放置在 /lib 与 /usr/lib 当中,所以您没有写 -L/lib 与 -L/usr/lib 也没有关系的!不过,万一哪天您使用的函式库并非放置在这两个目录下,那么 -L/path 就很重要了!否则会找不到函式库喔!

除了连结的函式库之外,您或许已经发现一个奇怪的地方,那就是在我们的 sin.c 当中第一行『 #include 』,这行说的是要将一些定义数据由 stdio.h 这个档案读入,这包括 printf 的相关设定。这个档案其实是放置在 /usr/include/stdio.h 的!那么万一这个档案并非放置在这里呢?那么我们就可以使用底下的方式来定义出要读取的 include 档案放置的目录:

[guest@test guest]# gcc sin.c -lm -I/usr/include

-I/path 后面接的路径( Path )就是设定要去搜寻相关的 include 档案的目录啦!不过,同样的,默认值是放置在 /usr/include 底下,除非您的 include 档案放置在其它路径,否则也可以略过这个项目!

透过上面的几个小范例,您应该对于 gcc 以及原始码有一定程度的认识了,再接下来,我们来稍微整理一下 gcc 的简易使用方法吧!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值