Chinaunix首页 | 论坛 | 博客
  • 博客访问: 313145
  • 博文数量: 51
  • 博客积分: 1975
  • 博客等级: 上尉
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-17 19:07
文章分类

全部博文(51)

文章存档

2011年(8)

2010年(43)

分类: LINUX

2010-01-16 22:39:13

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);
}
# 上面这个档案的内容可以在底下取得!
#  
 
那要如何编译这支程序呢?我们先直接编译看看:
  [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 档案放置在其它路径,否则也可以略过这个项目!
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tylovemx/archive/2009/11/25/4872727.aspx
阅读(4261) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~