A header file usually has some safe guard using the #ifndef
directives(or similar) e.g:
//header.hpp
#ifndef HEADER
#define HEADER
//code
#endif
but, I have a confusion here, what if we do the following(consider the two file's source codes):
//file1.cpp
#include "header.hpp"
//somecode
and the file
//file2.cpp
#include "header.hpp"
//somecode
if we did something like this:
g++ file1.cpp file2.cpp -o mainfile
we'd get a single executable that would get a single executable with no duplication since the includes are checked at compile time.
But, what if we do:
g++ -c file1.cpp -o file1.o
g++ -c file2.cpp -o file2.o
g++ file1.o file2.o -o mainfile.o
What happens during the linking stage? Will the includes have conflict? What happens to the includes during the compile time? Does it get duplicated? What is the mechanism under the hood to deal with this at this stage?