简体   繁体   中英

C++ Include files

I understand that providing a definition in a header file allows other files to reference them and then the linker adds all the object files together and provides the definitions later.

Is this just so that we can reuse the implementation else where in other libraries?

If we didnt use header files or abused them and put all the code in them, ie The full implementation what would happen? Would it take longer when compiling each file as the full definition would need to be compiled on a peer file basis? Would it cause issues with the linker as the would be multiple compiled versions of the same implementaion?

How does this work with templates?

Blair

Any code placed in your header file gets compiled every time a new compilation target includes that header file, so at a minimum yes, you will take longer to compile. This is not the significant problem generally though.

Lets say you have this header:

int f_header() {
    return 0;
}

And these source files:

/* a.cpp */
#include "myheader.h"
int f_a() {
    return f_header();
}


/* b.cpp */
#include "myheader.h"
int f_b() {
    return f_header();
}

Now, when you compile each of these sources the compilation will succeed. However when you link your program, the linker will fail because you will have provided the function f_header() twice.

Templates are a special case since they are not actually code, but rather they provide a template for code to be generated based on different types. Though there is the appearance of duplication when multiple source files use them with the same types, the compiler is able to handle this special case for you.

Splitting up the code in source and header files was (is?) more or less historically required. It allowed the compiler to compile code units of big projects separately, where the whole project would have been too big to be compiled at once.

And it allows to cache compilation units that haven't changed, which further improved compilation time.

So technically, the compiler sees a whole set of different compilation units. If you provide multiple implementations of the same definition, it's nothing the compiler could tell you. But the linker will stumble over it, once it wraps up the compiled units to a single executable file.

Today, "modern" languages like C# or Java moved away from this historical constraints. But C++ is a complicated language, even for the compiler, so these advantages are still relevant somehow.

Compiler compiles one file at a time. The source file that compiler is parsing may include other files, but all those included files are "flatten" into single source during pre-compile phase, and the compiler "sees" one single code entity.

Because of the above it does not matter to the compiler if the code is in .cpp or in .h file, because the compiler does not make that distinction. But if some function definition or some variable definition is in .h file the problem ensues in link phase.

Linker does not see source files, but it does see object files (.obj), which are the result that compiler produces. Linker is aware of every function and every variable in each object file, and linkers job is to bind (or link) them all together in a single executable. If there are several variables or functions with the same name in different object files linker is at loss of what to do with them, and it will declare an error. It is possible to force linker with an option to ignore all extra variables and functions, but that option is not to be used lightly. Two functions with the same name may actually be completely different, and ignoring one of them will lead to all sorts of errors.

Templates on the other hand are not functions at all. They are blueprints for actual functions. Nothing substantial will happen when compiler encounters a template. Only when a template is instantiated to an actual function the compiler will make such a function. Because of this templates can reside in header files just fine.

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