What is the difference between a header file and a library?

One of the things that I find difficult to understand is how the compiler works. I have a lot of difficulties with this, but, in particular, I mix headers and libraries all the time. If someone can figure it out a bit, it will be great.

+55
c ++ c
May 29 '09 at 5:17
source share
14 answers

Think about all of these (Disclaimer: this is a really high-level analogy;) ..

  • Headline is a telephone number that you can call while ...
  • ... the library is the person you can contact there!

This is the fundamental difference between an “interface” and an “implementation”; the interface (header) tells how to invoke some functionality (not knowing how it works), while the implementation (library) is the actual functionality.

Note. The concept is so fundamental that it allows you flexibility: you can have the same title for different libraries (i.e., the functionality is called exactly the same way), and each library can implement the functionality differently, Keeping the same interface, you can replace libraries without changing your code.

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

+90
May 29 '09 at 5:22
source share

Header file commonly used to define an interface or set of interfaces in an application. Think of the header file as something that shows the external functionality of the program, while omitting the details of the technical implementation.

For example, if you optimized the program, you are likely to modify the source file (.cpp) to improve the algorithm, but the header file will not change, because external clients still call methods using the same set of parameters and return values.

In an object-oriented language such as C ++, the header file usually includes the following:

  • Class Description and Inheritance Hierarchy
  • Members and class data types
  • Class methods

While there is no stop code in the header file, this is usually not recommended as it may introduce additional additional relationships and dependencies in the code.

In some cases (for example, template classes), the implementation must be defined in the header file for technical reasons.




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

Code is defined in the library to prevent code duplication and encourage reuse. 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 in the final executable (.exe) at the stage of linking the build process. This has the advantage of faster execution time (since the library does not need to be dynamically loaded) due to the larger binary file (since the methods are essentially replicated to the executable).

  • A library with dynamic linking is associated with the execution of the program, not with the binding of the program. This is useful when several programs must reuse the same methods and are widely used in technologies such as COM.

+42
May 29 '09 at 5:25
source share

One thing that can confuse you is that a word library can have multiple meanings in C ++. One meaning was discussed here:

A binding set of functions in a binary file. They can be statically linked or dynamically linked.

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

Hope this helps.

+7
May 29 '09 at 6:16
source share

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

The header file defines the interface for the library: it tells you how to use the library correctly. In C / C ++, the header file gives you a list of function names and methods of calling these functions: the number and types of parameters that they accept, return type, calling convention, etc. Header files have many other things in them too, but in the end, what he throws is a set of rules for invoking library code.

+6
May 29 '09 at 5:23
source share

The header contains only the declaration, while the libraries also contain the implementation.

+5
May 29 '09 at 5:23
source share

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

+1
May 29 '09 at
source share

If the library in programming languages ​​is a common library, then many books presented in the library can be compared with functions / methods in languages. And also the header files can be compared with the line number of the book. Suppose there is a book in some library in Hyderabad and in this library, this book is present in line No. 24 ... In the same way, the library address is set using the std namespace (for standard libraries), and the line No is set by the header file, where all books (methods in this case) of the same time (all methods associated with input / output streams) are set

+1
Mar 04 '13 at 13:35
source share

A HEADER FILE is the one in which the function declaration is written. Using the header file, we can access a specific function

but

LIBRARY FILE is the one in which the definition of a particular function is written. MATH.H is the HEADER FILE, and MATH.LIB is the library file.

+1
Jan 17 '14 at 12:34
source share

Work with the HEADER file and the library in the program.

A header file contains links to libraries (libraries contain standard functions and methods), the compiler recognizes standard functions used in the source code through a preprocessor that resolves all directives (directives are lines in the program preceded by a # sign that includes) before actual compilation of the program.

Thanks for reading!

+1
Jan 28 '14 at 9:50
source share

I think that the library is a package of code that is reusable, and this code is precompiled, so it is available in standard form, so we do not need to write this code for each program we develop. And the header file simply contains a link to this code, the functions that we use in our program, such as "cin" and "cout", are completely defined in the standard library, and header files, such as the iostream header file, contain a link to this the code. Therefore, when we compile our code, we simply get precompiled for cin and cout, and we do not need to write code for cin and cout for each use. Or in a simpler way, we can say that the library contains codes for all functions, and the header file is a way to achieve this code.

+1
Dec 30 '15 at 15:03
source share

A library is a collection of similar objects for random use. It usually contains programs in the form of an object or source code, templates, etc.

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

+1
Aug 25 '16 at 1:30
source share

To rephrase the classic joke, the difference is that the library has a header file and the header file does not have a library.

0
May 29 '09 at 6:42
source share

Libraries look like dead mummies wrapped in long white streams. They are dead. The only way to release them is with header files. Header files contain ways to animate them, and they can be animated many times (code reuse).

0
Mar 03 '17 at 10:04 on
source share

Code from libraries will be saved only for the header file. The entire header file will be saved, which saves the processor storage area.

-3
Jun 18 '17 at 15:27
source share



All Articles