Libraries and headers in c ++


I'm looking for information about C ++ libraries, but when I've read them I see missing headers like windows.h. What is the difference between a library and a header?

asked by Jogofus 25.05.2017 в 03:55

4 answers


A header file (which in the C ++ literature is called header ) is a file that contains mainly function declarations, type definitions and macros. Provide the API with which you access the functions, classes and macros of a library.

A "library" is actually a biblioteca , only sometimes it is called a library because it is written similar to "library", in English, but do not let it confuse you; when someone talking about C ++ says "library" is referring to a library, which is a collection of classes and functions. A library is usually composed of object files (already compiled) and "header" files, which are "included" (#include) in your .cpp and .h files.

Example prayer:


In order to use cout in your program, you must include the file of   header <iostream> , which is a file that is part of the    biblioteca estándar of C ++.

answered by 25.05.2017 в 09:07

What is the difference between a library and a header?

  • A library could be considered a collection of headers.
  • A header is a header file (header), which in C ++ has an extension of hpp 1 , normally (ideally) it only has statements of functions or objects.

Sometimes libraries are provided by third parties and contain, in addition to headers, pre-compiled code in the form of dll or lib to be used for link (dynamic or static link, respectively).


I see missing headers like windows.h

C ++ is a compiled language so once the program is generated, it is not modified. This means that the same code can not be adapted to different operating systems (unlike the languages interpreted or those that use virtual machine ), but to use it in different operating systems Compiled with the headers of this operating system.

Usually the compilers installed in the system already provide the necessary headers for it, otherwise the installation is incomplete, erroneous or there is a configuration failure. You should check the details of your compiler to be sure.

1 Although the extension is indifferent, an arbitrary extension can be used.

answered by 25.05.2017 в 09:15

What is the difference between a library and a header?

In C ++ you could integrate the entire program into a single file ... but this leads to a series of drawbacks:

  • The file ends up being slightly readable when different functionalities are mixed
  • Reuse of the code in new projects is too complicated
  • It is almost impossible to separate the application by layers
  • Any change involves recompiling all the source code (and this process in C ++ is not particularly fast).

It is for these reasons that the program is divided by functionalities or classes. Even so, imagine that, for example, in the case of classes, both the declaration and the implementation are left in a single file ... this practice has certain drawbacks:

  • Any change involves recompiling all the source code
  • If the application or library will be able to be used by a third party, you will offer all the algorithm ... something that is usually the most valuable part of a project.

So what is usually done is to separate the declarations of the implementations. When doing this division, a change in the implementation (for example the correction of an error), only entails the recompilation of a file.

Strictly speaking, C ++ does not determine which format extension each file should have. However, a series of conventions have been acquired over time. So:

  • Header files usually have the extension .h or .hpp, the second being more recommended (to distinguish between headers compatible with C)
  • The code files usually have the extension .c or .cpp, being the second most recommended again.

I see missing headers like windows.h

The pages with documentation about the bookstores usually cover only the set of libraries contemplated by the standard.

The headings of the Operating System or third parties are usually not covered by these pages. This documentation is usually found on the author's own page. So, for example, to investigate about WinAPI, go to the MSDN and to investigate about Boost, we went to your website .

It does not make sense that these libraries are documented in generic pages for several reasons:

  • Its modification cycle is different from that supported by the standard library
  • The libraries have their own programming / configuration / use policies
  • Bookstores do not have to be available in any Operating System

So, whenever you need to get out of the standard, you will have to resort to alternative sources of documentation to know the new architecture you are going to use.

answered by 25.05.2017 в 09:48

During the construction of the application, the preprocessor includes the header files in the sources. Subsequently, during the linking phase, the linker includes in the executable the modules corresponding to the functions and library classes that have been used in the program, so that the whole becomes part of the executable.

That's the main difference.

With the two libraries can be implemented.


answered by 25.05.2017 в 04:10