What are Header Files and Library Files?

56,725

Solution 1

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.

Solution 2

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:

  1. Header files are TEXT files while library files are BINARY. This means, we can read and modify the header file but not the library!
  2. Header file is in C language while the library is in machine language!
  3. Header file has to be included by the programmer while the compiler automatically relates the library file(s) with the program!

Solution 3

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.

Share:
56,725
karthik
Author by

karthik

Updated on June 20, 2020

Comments

  • karthik
    karthik almost 4 years

    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??

    • Lightness Races in Orbit
      Lightness Races in Orbit almost 13 years
      Please, search before asking. This has been covered a billion times.
    • Raj
      Raj over 12 years
      I have written article about header file, if you think that can help you in any way then visit rajkishor09.hubpages.com/_viki/hub/…
  • karthik
    karthik almost 13 years
    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 dont we say -lmath or -lstdio ??any good reason
  • karthik
    karthik almost 13 years
    thank you ozair but why we use that in command line only for threads why dont we use it for other header files thats my question
  • karthik
    karthik almost 13 years
    i like your explaination pankaj you gave a clear view really you did but do u 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
  • Piotr Praszmo
    Piotr Praszmo almost 13 years
    @karthik: gcc automatically adds -l flags for standard C libraries. That means functions declared in stdio.h, stdlib.h etc.
  • paxdiablo
    paxdiablo almost 13 years
    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.
  • karthik
    karthik almost 13 years
    but we declared pthread.h isnt that a standard c library?why dont the compiler automatically add for pthread and why only for stdio.h and stdlib.h
  • Ozair Kafray
    Ozair Kafray almost 13 years
    @Karthik: I have now added details to my answer, based on your comment.
  • nos
    nos almost 13 years
    @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.
  • Pankaj Kumar
    Pankaj Kumar almost 13 years
    @karthik adp-gmbh.ch/cpp/gcc/create_lib.html you should follow this.
  • Ozair Kafray
    Ozair Kafray almost 13 years
    @Karthik: I have now also included a reference to what is included in the standard C library. And notably it does not include pthreads.
  • Piotr Praszmo
    Piotr Praszmo almost 13 years
    @karthik: If you are still not sure. gcc doesn't really know the relation between headers and libraries. It just has hard-coded some -l flags with basic stuff.
  • Ozair Kafray
    Ozair Kafray almost 13 years
    @karthik: That's good, so now you shall accept the best answer.