#include "stdio.h"
#include "file_created_by_me.h"
In the first include there is a library file, in the second include there is a header file, or both can be called as libraries or headers because library and header are synonymous?
CodePudding user response:
Both are header files.
The first header file provides declarations of some of the functions provided by the C standard library. Therefore, one could say that it is part of the C standard library.
The second header file probably provides declarations of functions that are defined in other parts of your program. Therefore, it is not part of a library.
A library generally provides header files so that you can #include
them in your program, so that the compiler has the necessary function declarations in order to call the functions in the library. However, the main part of the library, which consists of the actual function definitions, is not imported by the compiler, but rather by the linker.
CodePudding user response:
The difference is that header files contains (as said above) some function declarations, library files contains execution code of this functions. Simple example: myfunc.h - header file
#ifndef MYFUNC_H
#define MY_FUNC_H 1
extern void myfunc(void);
#endif /* MYFUNC_H */
myfunc.c - definition of function
#include <stdio.h>
#include "myfunc.h"
void myfunc(void) {
printf("Hello world!\n");
}
This compiles by example with gcc by this way:
gcc -Wall -c myfunc.c
and produced file is myfunc.o - this is oure library And main program:
#include "myfunc.h"
int main(void) {
myfunc();
return 0;
}
Compile it:
gcc -Wall main.c myfunc.o
and run:
a.exe
in Windows or
./a.out
in *NIX systems...