Compiling with header files

Why do I need to specifically compile the C source file with

gcc prog.c -lm

even if I already included a specific header file:

#include <math.h>
+3
source share
4 answers

The file #includetells the compiler what the function looks like, in what type it returns, how many parameters, what types it accepts, but does not tell the compiler the contents.

The flag -lmincludes a real math library that contains code for the functions to be called.

printf(), fread() . stdio.h, , . C , .

+5

, , math.h.

+2

, (.h) (.c) , . , , :

double sqrt(double n);

However, it does not contain anything about how these functions work. This code is in a separate file that you must connect to, just as you link different source files to create an application.

+1
source

Because in C, it is technically absurd to have no connection between the header file and the library. There may be more header files than libraries, or vice versa. It is simply a matter of agreement (and, of course, it makes sense) to have a 1: 1 ratio in most cases.

0
source

Source: https://habr.com/ru/post/1769872/


All Articles