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 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.
Thank you paxdiablo but a small question why we include -lpthread in command line when we use posix threads but why not for the other header files like stdio. H math. H why don't we say -lmath or -lstdio?
Any good reason – karthik Jun 20 at 7:42 Actually, @karthik, you do sometimes have to specify -lmath (for those environments where they have it separate from the C RTL). But the reason why you don't normally have to do it is explained in the last paragraph of my answer - I'll expand on that a bit. – paxdiablo Jun 20 at 7:49 @karthik Most standard C and posix functions are in the standard C library, which is linked in by default, while others (such as pthread function or some of the math functions) reside in other libraries which have to be linked in explicittly.
– nos Jun 20 at 7:59.
The header files only include the definition of the functions that you would use in a file where the header file is being included. Library files comprise the actual implementation of the functions that you will be using in your program. The header file is included (copy/pasted) during the preprocessing stage and is compiled as part of the program being written during compilation phase.
One has to specify the -lpthread in the command line, so that the linker will know which library to look into for functions used in the program. Similar Question/Answer on Stackoverflow explaining it in layman terms: What's the difference between a header file and a library? Part 2: Why we do not have to always include library files when we have #include?
This might be the case when: i. The implementation of the functions is included in the header file. Ii.
The implementation of the functions is in c files for which you have the source available. Iii. The required libraries are included by your compiler by default e.g. , standard c libraries.
NOTE: Here is a reference to what is included in the standard C library, which is included by default by many compilers.
Thank you ozair but why we use that in command line only for threads why don't we use it for other header files thats my question – karthik Jun 20 at 7:43 @karthik: gcc automatically adds -l flags for standard C libraries. That means functions declared in stdio. H, stdlib.
H etc. – Banthar Jun 20 at 7:49 but we declared pthread. H isnt that a standard c library? Why don't the compiler automatically add for pthread and why only for stdio.
H and stdlib. H – karthik Jun 20 at 7:52 @Karthik: I have now added details to my answer, based on your comment. – Ozair Kafray Jun 20 at 7:52 @Karthik: I have now also included a reference to what is included in the standard C library.
And notably it does not include pthreads. – Ozair Kafray Jun 20 at 8:07.
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: 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!
I like your explaination pankaj you gave a clear view really you did but do you know how do I create my own library what extension does a library file have example I have one headerfile stringi. H and I have wrote those functions definations in a file then I want that file to be a library file so that I can use later but how do I make a file as a library file for future use – karthik Jun 20 at 7:46 @karthik adp-gmbh. Ch/cpp/gcc/create_lib.
Html you should follow this. – Pankaj Kumar Jun 20 at 7:59.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.