Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly happens when a header file is included?

I have two doubts:

  1. What does a header file actually contain? All the function definitions or only the prototype declarations?
  2. What happens when I include a header file? Are all the contents of the header file attached to my code? Or is it that the specific (or all) contents of header file is loaded into the memory and the functions are called according to my code?
like image 785
user2684198 Avatar asked Jun 30 '14 06:06

user2684198


People also ask

Why should we include header file?

Why Do You Use Header Files? Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .

What is header file and how it works?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

What happens if header file is not included?

Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef. When the header is included again, the conditional will be false, because FILE_FOO_SEEN is defined. The preprocessor will skip over the entire contents of the file, and the compiler will not see it twice.

Do you need to include in header files?

Such project header files should contain #include statements for the system headers; if the body includes them first, the compiler does not check this.


1 Answers

C and C++ are languages, which extensively uses feature called function forwarding. This means, that you can say, for instance:

void f(int i); /* note the semicolon */

This means: "I promise, that somewhere later in the source code someone will define, what the function f actually does". After such forward, you may use this function as it really existed and compiler will require someone to actually define this function later (if there is no definition, compilation will fail). This forward is called also a header, because it actually is a header of function definition:

void f(int i) /* Header */
/* Body */
{   
    /* ... */
}

Header file is a file, which contains mostly such forwards. You can use the header file to get access to functions defined somewhere else (for example in different compilation unit or external library) and then attach required object files or libraries to provide implementation of these header files. Apart from function forwards, in a header file you may also find structure definitions, constants or other items, which are required for proper use of defined functions.

How compiler matches your forward to actual implementation in .c file? Well simply - by the header. It attempts to find a function definition (implementation), which exactly matches the header you declared earlier.

What happens if you #include header file is that compiler (specifically, the preprocessor) copies the whole contents of header file into place, where you put your #include. That's all magic, nothing more happens.

During runtime, the header file does not matter at all, because your executable file consists only of executable code. Compiler either loads all functions available in the library (which is accessed by the header file) or (mostly probably, if optimization is turned on) chooses only these functions, which you actually used in your code.

The funny thing is, that compiler requires function definition (implementation) only if someone actually uses that function. Otherwise, the forward is ignored. Try:

void f(int i);

int main(int argc, char ** argv)
{
    /* Do not use f here */

    return 0;
}
like image 60
Spook Avatar answered Nov 15 '22 02:11

Spook