16

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.

Why does GCC does include this library by default? Is it because the library heavily uses a 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 question gcc: why is the -lm flag needed to link the math library?. 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 does it achieve? Here is what I tested, without libm and with libm. For the one without libm, I have written my own version of Pow().

Here is the example:

cd ~/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;
    }

$ gcc Test_withlibm.c -lm -o Main_withlibm.o
$ gcc Test_withoutlibm.c -o Main_withoutlibm.o
$ objdump -d Main_withoutlibm.o|wc -l

261

$ objdump -d Main_withlibm.o|wc -l

241
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 2
    Historical reasons, I guess. The linker should be easily able to not link functions that aren't used. MSVC doesn't need a libm either for you to use the math functions. – Joey Apr 29 '12 at 11:14
  • Direct duplicate (merge action): *[Why do you have to link the math library in C?](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c)* – Peter Mortensen Oct 27 '22 at 23:43

3 Answers3

12

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

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

Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48
2

Historical reasons

The reasons libc and libm are separated and you have to specify -lm on the command line are historical reasons because the libm was also used by the Fortran compiler.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 2
    So the same libm can be shared by Fortran and C was the reason to separate? When linking Fortran program did we have to link it explicitly with `-lm` as we go with gcc? – Abhijit Apr 29 '12 at 12:21
1

There are many libraries you may want, and libm is just one of them.
For each of these, you may ask why it isn't included by default.

Perhaps libm is more useful than others, but still, C prefers to keep things simple. If you want a library, use -l to use it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ugoren
  • 16,023
  • 3
  • 35
  • 65
  • 7
    But what's unique about `libm` is that it's part of the C standard library. AFAIK, it's the only part of the C standard library that isn't linked by default. – Oliver Charlesworth Apr 29 '12 at 11:36
  • 1
    @OliCharlesworth, good point. It would be nice if "standard" and "linked by default" were the same thing. – ugoren Apr 29 '12 at 11:40
  • 1
    I don't buy the "keeping things simple" argument. They could've kept things even more simple by not linking `libc` by default. You don't need it in every program, just watch: `main(){}` - see? Why would I want to link `libc` to it? – Tomasz Gandor Dec 11 '18 at 20:20