为什么你需要一个明确的`-lm`编译器选项 [英] Why do you need an explicit `-lm` compiler option

查看:151
本文介绍了为什么你需要一个明确的`-lm`编译器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

gcc:为什么需要-lm标志链接数学库?


一般来说,为了使用除了包含头文件 math.h 您必须链接链接器选项-lm。 -l <​​/ code>这里意味着链接器选项来搜索特定的库 libm.o



我的问题是

为什么GCC默认不包含这个库?这是因为库大量使用数学协处理器,它需要添加额外的代码位来初始化浮点初始化(我可能会在这里使用错误的术语)?

注意

我刚刚回顾了链接中提到的所有答案http://stackoverflow.com 。这对我来说没有多大意义。有三个基本的原因归因于:b
$ b


  1. 标准库保证可用。链接像pthread这样的其他posix库显然是有意义的,但为什么我们必须为标准库做一个明确的链接。即使是历史原因也不是很清楚。
  2. 为什么libm与libc分离?

  3. 为什么我们仍然在最近的gcc编译器中继承这些行为?它实现了多少简单?这是我测试的,没有libm和libm。一个没有libm的,我写了我自己的Pow版本

    $ b $ p
  4. 以下是例子

      abhibhat @ abhibhat-VirtualBox:〜/ Projects / GIPL6_2 $ ls -1 Test_ * | xargs -I {} sh -cecho {}&& echo -----------------&& cat {}
    Test_withlibm.c
    ------------- ----
    #include< stdio.h>
    #include< math.h>
    int main(){
    int i = 20;
    double output1 = pow(2.618033988749895,i);
    返回0;
    }
    Test_withoutlibm.c
    -----------------
    #include< stdio.h>
    #include< math.h>
    double Pow(double _X,int _Y){
    double _Z = 1; (; _Y; _X * = _X){
    if(_Y& 1)_Z * = _X;
    _Y>> = 1;
    }
    return _Z;
    }
    int main(){
    int i = 20;
    double output1 = Pow(2.618033988749895,i);
    返回0;

    abhibhat @ abhibhat-VirtualBox:〜/ Projects / GIPL6_2 $ gcc Test_withlibm.c -lm -o Main_withlibm.o
    abhibhat @ abhibhat-VirtualBox:〜/ Projects / GIPL6_2 $ gcc Test_withoutlibm .c -o Main_withoutlibm.o
    abhibhat @ abhibhat-VirtualBox:〜/ Projects / GIPL6_2 $ objdump -d Main_withoutlibm.o | wc -l
    261
    abhibhat @ abhibhat-VirtualBox:〜/ Projects / GIPL6_2 $ objdump -d Main_withlibm.o | wc -l
    241


    解决方案

    这是容纳浮点数学不可能或不必要的系统(主要是嵌入式)。这确实是一种历史,但不要忘记,在386SX被认为是高性能的处理器的时候写了 gcc 和大多数其他的C编译器。

    $举个例子,当我还在嵌入式计算领域工作时,我们使用标准编译器(Microsoft和Borland)为我们的处理器(Z80,80186和68030)生成代码。如果编译器默认链接到数学库,那么我们将遇到麻烦,因为我们的系统中没有一个具有浮点功能,甚至不需要它。

    事实上,30年后似乎很愚蠢,但当时的原因是正确的。

    Possible Duplicate:
    gcc: why the -lm flag is needed to link the math library?

    Generally speaking, in order to use any of the math functions apart from including the header file math.h you have to link with the linker option -lm. -l here would mean the linker option to search of the specific library libm.o.

    My Question is

    Why GCC does not include this library by default? Is it because the library heavily uses math-coprocessor and it requires to add the extra bit of code to initialize the floating point initialization (I may use the wrong terminology here)?

    Note

    I just reviewed all the answers mentioned in the link http://stackoverflow.com. This doesn't makes much sense to me. There are three basic reasons attributed

    1. The standard libraries are guaranteed to be available. Linking other posix libraries like pthread explicitly makes sense, but why do we have to do an explicit link for a standard library. Even the historical reason is not very clear.
    2. Why was libm separated from libc?
    3. Why are we still inheriting these behaviors in the recent gcc compilers? What simplicity it achives? Here is what I tested, without libm and with libm. The One without libm, I have written my own version of Pow

    Here is the example

    abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ ls -1 Test_*|xargs -I{} sh -c "echo {} && echo "-----------------" && cat {}"
    Test_withlibm.c
    -----------------
    #include<stdio.h>
    #include<math.h>
    int main() {
        int i=20;
        double output1=pow(2.618033988749895,i);
        return 0;
        }
    Test_withoutlibm.c
    -----------------
    #include<stdio.h>
    #include<math.h>
    double Pow(double _X, int _Y)  {
        double _Z = 1;
        for (; _Y; _X *= _X) {
        if (_Y & 1) _Z *= _X;
        _Y >>= 1;
        }
        return _Z; 
        }
    int main() {
        int i=20;
        double output1=Pow(2.618033988749895,i);
        return 0;
        }
    abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ gcc Test_withlibm.c -lm -o Main_withlibm.o
    abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ gcc Test_withoutlibm.c -o Main_withoutlibm.o
    abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ objdump -d Main_withoutlibm.o|wc -l
    261
    abhibhat@abhibhat-VirtualBox:~/Projects/GIPL6_2$ objdump -d Main_withlibm.o|wc -l
    241
    

    解决方案

    It is to accomodate systems (mainly embedded) where floating point math is not possible or necessary. It's kind of historical indeed but don't forget that gcc and most other C compilers were written in a time where a 386SX was considered a high performance processor.

    To give an example, when I still worked in embedded computing, we used standard compilers (Microsoft and Borland) to generate code for our processors (Z80, 80186 and 68030). If the compilers had by default linked to the math library we would have been in trouble as none of our systems had floating point capabilities or even needed it.

    It's true that 30 years afterwards it seems silly but the reason was sound at that time.

    这篇关于为什么你需要一个明确的`-lm`编译器选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆