Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling with header files

Tags:

c

gcc

Why do I have to specifically compile a C source file with:

gcc prog.c -lm

even when I have already included the specific header file with:

#include <math.h>

2 Answers

The #include file tells the compiler how a function looks like, as in what type it returns, how many parameters of what types it takes, but it doesn't tell the compiler the contents.

The -lm flag includes that actual math library which contains the code for the functions to be called.

It works the same way with printf(), fread() and other standard functions. When you include stdio.h, you don't actually include the code of the function but the definitions. Because the C library is implicitly linked without you having to do anything about that, you don't notice it.

like image 169
LukeN Avatar answered Aug 08 '26 11:08

LukeN


Because you need to inform the compiler which math library to link with, nothing to do with the math.h inclusion.

like image 33
Otávio Décio Avatar answered Aug 08 '26 12:08

Otávio Décio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!