How Does #Include Work in C++

How does #include work in C++?

No, #includes are transitive.

However, if your second file itself uses symbols from someLibrary, it's good style to re-include the header. That way you're not "hoping and praying" that you never remove the intermediate include. Your codebase will be more robust if every source file #includes everything that it directly needs. Header guards prevent this from being a wasteful policy.

What does #include actually do?

Logically, that copy/paste is exactly what happens. I'm afraid there isn't any more to it. You don't need the ;, though.

Your specific example is covered by the spec, section 6.10.2 Source file inclusion, paragraph 3:

A preprocessing directive of the form

# include "q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters.

When to use #include file.c and when to use #include file.h in c

  1. Never include .c files
  2. If you include something, it should be a .h file.
  3. Code goes into code files, .c.
  4. Headers, .h files, contain only what can be compiled more than once, i.e. declarations, macro definitions, more includes.
  5. If necessary, headers use reinclusion guards.

Whenever you feel tempted to include a .c file, you instead want to add it to your project so that it gets processed during building, i.e. gets compiled by its own and in a further build step gets linked into the created binary.

What exactly happens when a header file is included?

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 is C Header Linguistically?

a header is a file with the .h extension.
It is meant to help the compiler to understand the symbol (method, function, struct, variable...) that are used within the code. See of it like an glossary at the end of a book.
It is used solely for development purpose.
It helps develloper knowing what function are available without having to look in all the implementation file. Like a man page.

A search on google? http://www.tutorialspoint.com/cprogramming/c_header_files.htm :)

About #include mechanism in c++

Per C++ standard ([cpp.include]/3):

A preprocessing directive of the form

# include " q-char-sequence " new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified
sequence between the " delimiters. The named source file is searched for in an implementation-defined
manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

# include < h-char-sequence > new-line

with the identical contained sequence (including > characters, if any) from the original directive.

So the answer to your question:

This makes me curious about the include mechanism, is the #include derivative simply take everything in the included file and put them at the #include before compiling?

Is simply "yes".

About naming of the files, as someone already pointed out in the comments, extensions are just part of the filename. They do not play any role in the interpretation of a file by the C++ compiler. As such, you can name them absolutely whatever you want. You can even not include an extension, as the STL does.



Related Topics



Leave a reply



Submit