5

I'm confused why you have to type -lm to properly link math to your code, but don't have to do the same for stdio. I've only just started using C, so I apologize if this is a stupid quesiton or I'm missing something obvious.

Andrew0085
  • 85
  • 1
  • 4
  • 3
    http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c?rq=1 – verbose Aug 05 '13 at 06:23
  • `*.h` files are header files, header files are **not** linked, headers are **`include`ed**! – alk Aug 05 '13 at 06:25

2 Answers2

7

In short, because of historical reasons,

The functions in stdio.h are in libc, while the functions in math.h are in libm. libc is linked by default but libm isn't.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

There are two different things:

  • header files (stdio.h and math.h) - they contain only function prototypes and some definitions and data; they are #included in your source code
  • libraries (libm.so) - they contain binary code which will be linked back into your application (binary code). Also, for a library named libname.so the linker flag is -lname - for libm.so the flag is -lm.

Take also in consideration that there are libc.so and libstdc.so which are always linked into your application. Code for functions in stdio.h and stdlib.h and several others is found on those libraries - thus, it is always included.

PS: I'm assuming Linux/UNIX here, thus the names are very specific. On Windows things are similar but with other names (DLLs instead of .so files, etc.)

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109