[ACCEPTED]-What are Header Files and Library Files?-posix
Generally, a header file notifies the compiler of 28 certain things (mostly their existence or 27 declarations) so that the compiler can correctly 26 build a single translation unit (such as 25 a single C file).
A library file is the actual 24 executable code that does the work as specified in that 23 header file. This is linked in by the linker to 22 provide the actual functionality (the _definitions 21 rather than just the declarations).
So, in 20 your example, you may have the line:
#include <pthread.h>
which 19 tells the compiler all about the existence of the 18 pthread_mutex_this
, pthread_condvar_that
and pthread_thread_the_other
stuff but doesn't actually provide 17 the implementations of those things.
The 16 -lpthread
option tells the linker that it should 15 locate a library based on the pthread
name from 14 which it can pull in the actual implementations, in 13 order to forn the final executable.
Similarly, while 12 stdio.h
holds information about the I/O stuff, the 11 actual code for it will be in the runtime 10 library (though you rarely have to link 9 that library specifically since the compiler 8 will try to take care of it for you). Because 7 you usually link with the compiler (i.e., the 6 compiler invokes the linker for you), it 5 knows that you're probably going to need 4 the C run time library. If you were to use 3 the linker directly (such as by using the 2 ld
command), that probably wouldn't happen, and 1 you'd have to be explicit.
Header Files : These are the files that are included 14 at the top of any program. If we use any 13 function inside a program, then the header 12 file containing declaration or definition 11 of that function ,has to be included.Like 10 printf() is defined in stdio.h.So, we must 9 include it (by #include in order to use 8 printf().
Library Files: These are the files which the 7 compiler uses in order to define the functions 6 which have been used in the program and 5 had been declared inside the header file.Like, printf() has 4 its complete definition ,like how it will 3 work etc. in an I/O library! So, the compiler 2 uses that library to get the machine code 1 for printf.
Difference:
- Header files are TEXT files while library files are BINARY. This means, we can read and modify the header file but not the library!
- Header file is in C language while the library is in machine language!
- Header file has to be included by the programmer while the compiler automatically relates the library file(s) with the program!
The header files only include the definition 22 of the functions that you would use in a 21 file where the header file is being included.
Library 20 files comprise the actual implementation 19 of the functions that you will be using 18 in your program.
The header file is included 17 (copy/pasted) during the preprocessing stage 16 and is compiled as part of the program being 15 written during compilation phase. One has 14 to specify the -lpthread in the command 13 line, so that the linker will know which 12 library to look into for functions used in the 11 program.
Similar Question/Answer on Stackoverflow 10 explaining it in layman terms:
What's the difference between a header file and a library?
Part 2: Why we do 9 not have to always include library files when 8 we have #include
?
This might be the case when:
i. The 7 implementation of the functions is included 6 in the header file.
ii. The implementation 5 of the functions is in
c
files for which you 4 have the source available.iii. The required 3 libraries are included by your compiler by default e.g., standard c libraries.
NOTE: Here 2 is a reference to what is included in the standard C library, which is included by 1 default by many compilers.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.