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

Asked

Viewed 99 times

2

That is, the executable will have the entire library in it or only the functions I use will be included by the compiler ? With this, including many libraries can make the program heavy (taking up too much disk space) ?

1 answer

2


The header generally only includes function statements 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 time of link all the code generated by the compiler with the libraries used, it will not be included.

But it’s not that simple. Actually if you’re using some header functions and others aren’t, it might not make that much difference. It is very likely that all functions declared in the header used are defined in a single binary that cannot be separated. There you have to choose only a few functions, go everything that is in binary, there is no way to separate. Example: you used the function a() that is in a binary, did not use the function b() 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 function c() but it is not in the same binary. If it was not used effectively, it will not be included. Of course, it is rare for you to have declared functions in a header and they are in different binaries.

This binary is a compilation unit which cannot be divided, so if you use a member of this unit, all members of it will go along with your application. This binary is called object file (not to be confused with objects of memory or object orientation).

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

So if you include many libraries, yes the application will get heavier. But if you need them it doesn’t matter.

In some cases if you need a single function that is in a large build unit, it may be useful to take the function alone, if its source is available and the license permits, and use it separately. But in practice, this will rarely be important. In general, the compilation units are not as large or are assembled to include their own dependencies but nothing extra.

Note also that some functions may be in a DLL (or SO) and will not be included within your executable.

Information about libraries that may interest.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.