What's the difference between a header file and a library?

C++C

C++ Problem Overview


One of the things I'm having a hard time understanding is how the compiler works. I'm having a lot of difficulties with it, but in particular I keep getting headers and libraries mixed up. If somebody could clear things up a bit, that'd be great.

C++ Solutions


Solution 1 - C++

Think of both like this (Disclaimer: this is a really high-level analogy ;) ..

  • The header is a phone number you can call, while...
  • ...the library is the actual person you can reach there!

It's the fundamental difference between "interface" and "implementation"; the interface (header) tells you how to call some functionality (without knowing how it works), while the implementation (library) is the actual functionality.

Note: The concept is so fundamental, because it allows you flexibility: you can have the same header for different libraries (i.e. the functionality is exactly called in the same way), and each library may implement the functionality in a different way. By keeping the same interface, you can replace the libraries without changing your code.

And: you can change the implementation of the library without breaking the calling code!

Solution 2 - C++

A header file is generally used to define an interface or set of interfaces within an application. Think of a header file as something which shows the external functionality of a program while omitting the technical implementation details.

For example, if you were optimising a program, you would most likely modify the source (.cpp) file to improve the algorithm, but the header file wouldn't change, because external clients still call the methods using the same set of parameters and return values.

In an object-oriented language like C++, a header file generally includes the following:

  • Class description and inheritance hierarchy
  • Class data members and types
  • Class methods

While there is nothing stopping code from being implemented in a header file, this is generally not favoured as it can introduce extra coupling and dependencies in the code.

In some cases (e.g. templated classes) the implementation must be defined in the header file for technical reasons.


A library is a collection of code which you want to make available to a program or group of programs. It includes the implementation of a particular interface or set of interfaces.

Code is defined in a library to prevent code duplication and encourage re-use. A library can be statically-linked (.lib) or dynamically-linked (.dll):

  • A statically-linked library defines a set of export symbols (which can be thought of as method definitions) which are then linked into the final executable (.exe) during the linking stage of the build process. It has the advantage of faster execution time (as the library doesn't need to be dynamically loaded), at the expense of a larger binary (because the methods are essentially replicated in the executable file).

  • A dynamically-linked library is linked during the execution of a program, rather than the linking of a program. It is useful when multiple programs need to re-use the same methods, and is used extensively in technologies such as COM.

Solution 3 - C++

One thing that may be confusing you is that the word library can have several meanings in C++. One meaning has been well-discussed here:

A linkable set of functions in a binary file. These can be statically linked or dynamically linked.

But there is another type of library: so-called header-only libraries (including parts of STL, TR1 and Boost). These do not exist in a separate binary form so the word library does not refer to a particular binary file but rather to a set of included header files.

Hope this helps.

Solution 4 - C++

Header only contains the declaration, whereas libraries also contains the implementaion.

Solution 5 - C++

A library is code, compiled into a set of object files. The object files contain the compiled machine code and the data declarations used by the code.

A header file defines the interface to a library: it tells you how to use the library correctly. In C/C++, a header file gives you a list of function names and how to call those functions: the number and types of parameters they take, the return type, the calling convention, etc. Header files have a lot of other stuff in them, too, but in the end, what it boils down is a set of rules for calling library code.

Solution 6 - C++

If Library in programming languages is a general library , then many books present in the library can be compared with functions/methods in languages . And also header files can be compared with the row number of the book Suppose there is a book in some library in Hyderabad and in that library , that book is present in row Number 24 ... The same way the address of the library is given by using the namespace std (for standard library) and row No is given by header file where all the books(methods in this case) of same time(all methods related to input/output streams) are put up

Solution 7 - C++

Header is typically used to contain the prototypes. Headers expand at pre-processing time so that at compile time, the code may have access to relevant function declarations/ prototypes.

Library is the actual software that contains the definitions of the function prototypes (present in the header). Library is used at link time. The definitions (present in library) are resolved at link time.

Solution 8 - C++

A header file describes how to call the functionality, a library contains the compiled code that implements this functionality.

Solution 9 - C++

HEADER FILE is that in which declaration of a function is written.By using header file we can access a particular function

while

LIBRARY FILE is that in which definition of a particular function is written. MATH.H is a HEADER FILE while MATH.LIB is library file.

Solution 10 - C++

Working of HEADER File and LIBRARY in a Program.

A header file contains the links to libraries(libraries contain standard functions and methods), a compiler recognizes the standard functions used in the source code via a preprocessor, that resolves all the directives(directives are the lines in program preceded by # sign that include

) before the actual compilation of the program.

Thanks for reading!

Solution 11 - C++

I think library as a package of code which is reused many times and and that code is precompiled , hence it is available in standard form so that we do not have to write that code for every program that we develop . And header file contain the reference to that code in simple way the functions we use in our program like "cin" and "cout" are fully defined in a standard library ,and header files like iostream header file contains the reference to that code. So when we compile our code it we just get the precompiled for cin and cout, and we do not have to write the code for cin and cout for every time we use it. Or In a more simple way we can say that a library contain codes for all the functions and a header file is way to reach that code.

Solution 12 - C++

A library is a collection of similar objects for occasional use . It usually contains programmes in object or source code form, templates, etc.

A header file is the location (interface) of the library

Solution 13 - C++

To paraphrase a classical joke, the difference is that the library has a header file while the header file doesn't have a library.

Solution 14 - C++

Libraries are like dead mummies,wrapped in white long threads. They are dead. Only way to release them is through header files. Header files contain ways to bring them to life and they can be brought to life many times(code reuse).

Solution 15 - C++

You can consider this example to understand- Math.h is a header file which includes the prototype for function calls like sqrt(), pow() etc, whereas libm.lib, libmmd.lib, libmmd.dll are some of the math libraries. In simple terms a header file is like a visiting card and libraries are like a real person, so we use visiting card(Header file) to reach to the actual person(Library).

Solution 16 - C++

Code from libraries will only be stored as needed for a header file. The whole header file will be stored, which saves processor storage area.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXonaraView Question on Stackoverflow
Solution 1 - C++beef2kView Answer on Stackoverflow
Solution 2 - C++LeopardSkinPillBoxHatView Answer on Stackoverflow
Solution 3 - C++jwfearnView Answer on Stackoverflow
Solution 4 - C++Moayad MardiniView Answer on Stackoverflow
Solution 5 - C++Adam RosenfieldView Answer on Stackoverflow
Solution 6 - C++user2131930View Answer on Stackoverflow
Solution 7 - C++SyedView Answer on Stackoverflow
Solution 8 - C++sharptoothView Answer on Stackoverflow
Solution 9 - C++vipinView Answer on Stackoverflow
Solution 10 - C++Nauman KhanView Answer on Stackoverflow
Solution 11 - C++shivamView Answer on Stackoverflow
Solution 12 - C++Jayraj Srikriti NaiduView Answer on Stackoverflow
Solution 13 - C++Tiberiu AnaView Answer on Stackoverflow
Solution 14 - C++jatinpuniaView Answer on Stackoverflow
Solution 15 - C++LocalHostView Answer on Stackoverflow
Solution 16 - C++JerryView Answer on Stackoverflow