简体   繁体   中英

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?

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;
}

What does a header file actually contain? All the function definitions or only the prototype declarations?

Header file contain function declarations, extern variables,macros,structures etc. Best practice is to keep Function definition is in .c file.

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?

Header files are nothing more than inserting the contents of them at the place where you use #include . You can write all that on your own if you want to.

Including a header file is equal to copying the content of the header file but we do not do it because it will be very much error-prone and it is not a good idea to copy the content of header file in the source files, specially if we have multiple source file comprising our program.

Edit:

For memory layout of complete execution of program you can follow this link http://fgiasson.com/articles/memorylayout.txt

  1. The header file should contain useful declarations of functions (and perhaps variables) and definitions of types ( struct , union , enum or typedef ) and macros (function-like or object-like macros). These typically declare the facilities made available by some part of some library. The standard headers, such as <stdio.h> , describe portions of the standard library, for example. Modern headers might contain inline function definitions, but otherwise, a header should define neither functions nor variables — it should only declare them.

    Note, too, that a public header should normally only declare types and functions that are useful to the consumers of the source code. It should only include other headers that provide declarations that are necessary to make use of the facilities in the header; it should not (usually) include extraneous headers that are only needed by the implementation of the facilities described by the header.

  2. The contents of the header file (and any header files it includes) are copied into the translation unit (TU). The #define macros and conditional processing ( #if etc) are noted. You can usually find a way to see the output of the preprocessor on a given source file to see what the C compiler proper actually sees (but beware; it can be very verbose). For example, the options are often -E (required by POSIX for the c99 command) or -P (an older commonly used option).

The TU is what the compiler proper sees. It contains the source code from your file plus the relevant information from the headers you include.

Just a side note, it may be obvious...

  • ad 2/ If you #include a file the #include directive in the includig file is repalced by the included file. This is what happens. This is what the preprocessor does. By defintition

  • ad 1/ A header file can continue anything (anything the compiler can accept). Other answers describe the usual conventions used to organize code between c and h files, but these are only conventions. They are very good to stick with, you are likely to get lost with a bigger project if you dont.

Actually, if you write c for a very simple microcontroller, you may make it completly without header files and their including.

If you are a student/hobyist/have the time, it may be useful, to run the prepprocessor separately have a look on the itermediate output. Fun fact: Long time ago i used to use the c preprocesor to generate coustomized letters.

Anyway my point is: what c preporocesor does is defined by standard, while orgainizing code between files a convention which you/your team can select to a certain extent. This distinction is important.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM