When a header file is included, does the compiler include all the functions in the final program or only the functions used?

2

That is, is the executable going to have the whole library in it or only the functions that I use will be included by the compiler? With this, can you include many libraries that can make the program cumbersome (taking up a lot of disk space)?

    
asked by anonymous 09.04.2015 / 21:11

1 answer

2

The general header only includes the function declarations to provide important information to the compiler. Thus, if the function is not used, there is a chance that the final application will not be included. If there is no reference to the function, at the moment of link all code generated by the compiler with the libraries used, it will not be included.

But it's not that simple. In fact if you are using some header functions and some not, this may not make as much difference. It is very likely that all functions declared in the header used are defined in a single binary that can not be separated. There you do not have to choose only some functions, everything goes in the binary, there is no way to separate. Example: You used the a() function that is in a binary. You did not use the b() function that is in the same binary. Both will be present in your application even if you have only used one of them. But let's say that in the same header there is a c() function but they are not in the same binary. If it has not been used effectively, it will not be included. Of course, you rarely have declared functions in a header and they are in different binaries.

This binary is a compilation drive that can not be split, so if you use a member of this unit, all her members will go along with your application. This binary is called object file (not to be confused with object or memory object).

It is still important to note that if the function is defined in the header, it will certainly be in the application because it will be compiled there in time by the compiler and will be part of its own code compiled at that time and will not be part of the library used.

So if you include many libraries, yes, the application will become heavier. But if you need them, it does not matter.

In some cases if you need a single function that is on a large compilation drive, it may be useful to pick up the function in isolation, if your source is available and the license allows, and use it separately. But in practice, this will rarely be important. In general, the build units are not as large or are built to include their own dependencies but nothing extra.

Also note that some functions may be in DLL (or SO ) and will not be included inside your executable.

Library information you may be interested.

    
09.04.2015 / 21:19