Possible Duplicate:
What's the difference between a header file and a library?
Can anyone tell me what's the actual meaning of a header file and a library file and their difference?
For example we include header file with .h extension in our program and its just the definition but the actual implementation is defined in library files and this is done at linking stage this is what people say but sometimes we include the library files directory too for the programs to generate the exec file for example in posix threads people say to include the -lpthread in the command line but why when we included the header file #include<> why we still need to include the library files too may i know the reason please??
No. Header File is the file where all the headers name are mentioned that going to be used or consumed in the main code file. On other hand Library is the file where the implementation code of each header is written down which is mentioned in the Header file.
If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.
A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ' #include '. Header files serve two purposes.
A document library provides a secure place to store files where you and your co-workers can find them easily, work on them together, and access them from any device at any time. For example, you can use a document library on a site in SharePoint to store all files related to a specific project or a specific client.
Generally, a header file notifies the compiler of certain things (mostly their existence or declarations) so that the compiler can correctly build a single translation unit (such as a single C file).
A library file is the actual executable code that does the work as specified in that header file. This is linked in by the linker to provide the actual functionality (the _definitions rather than just the declarations).
So, in your example, you may have the line:
#include <pthread.h>
which tells the compiler all about the existence of the pthread_mutex_this
, pthread_condvar_that
and pthread_thread_the_other
stuff but doesn't actually provide the implementations of those things.
The -lpthread
option tells the linker that it should locate a library based on the pthread
name from which it can pull in the actual implementations, in order to forn the final executable.
Similarly, while stdio.h
holds information about the I/O stuff, the actual code for it will be in the runtime library (though you rarely have to link that library specifically since the compiler will try to take care of it for you). Because you usually link with the compiler (i.e., the compiler invokes the linker for you), it knows that you're probably going to need the C run time library. If you were to use the linker directly (such as by using the ld
command), that probably wouldn't happen, and you'd have to be explicit.
Header Files : These are the files that are included at the top of any program. If we use any function inside a program, then the header file containing declaration or definition of that function ,has to be included.Like printf() is defined in stdio.h.So, we must include it (by #include in order to use printf().
Library Files: These are the files which the compiler uses in order to define the functions which have been used in the program and had been declared inside the header file.Like, printf() has its complete definition ,like how it will work etc. in an I/O library! So, the compiler uses that library to get the machine code for printf.
Difference:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With