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.linux


--------------------------------------------------------------------------------ide

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 .ui

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


--------------------------------------------------------------------------------code

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 .blog

当使用gcc编译器编译含数学函数的C程序时,会出现undefined reference to `sin'错误.这种错误通常是因为缺乏库形成的.使用-lm便可.Makefile能够这样写:ci

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-11get

具体缘由及解决办法为:编译器

加入连结的函式库

刚刚咱们都仅只是在屏幕上面印出一些字眼而已,若是说要计算数学公式呢?!例如咱们想要计算出三角函数里面的 sin(90度角),要注意的是,大多数的程序语言都是使用径度而不是通常咱们在计算的『角度』, 180 度角约等于 3.14 径度!嗯!那咱们就来写一下这个程序吧!
  [guest@test guest]# vi sin.c
#include <stdio.h>
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> 』,这行说的是要将一些定义数据由 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 的简易使用方法吧!

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tylovemx/archive/2009/11/25/4872727.aspx

相关文章
相关标签/搜索