c++nested-includes

Nested header inclusion prolongs compilation


In my project I am using a third-party lib which is included dynamically - i. e. via an import *.lib-file and an *.h-file. The *.h-file of the lib has an include guard. I #included this file in one of my project headers, which starts from #pragma once. The latter in its turn is also included in several *.cpp-files of my project.

Here is the scheme:

third-party lib include 'blah_blah.h'

#ifndef BLAH_BLAH_H
#define BLAH_BLAH_H

/* stuff here */

#endif

my project header 'my.h'

#pragma once
#include "blah_blah.h"

/* stuff here */

one of my cpp files

#include "my.h"

/* stuff here */

The problem is the following. Although there are neither errors nor warnings during compilation, I see that "blah_blah.h" gets included several times - at least, the warnings, which are produced by its code, appear in the output window of Visual Studio 2017 about 5 times and the compilation lasts like forever. What can I do to avoid this?


Solution

  • is also included in several *.cpp-files of my project.

    if you compile for instance 2 sources including directly or indirectly your header that one will be included 2 times at total. The protection is only for one source file, each time the compiler moves to an other source file to compile none of the header are known. Else that means the order of the source file compiled is relevant, this is not possible to manage, this is the end of the makefile and force to recompile all files whatever the file(s) you modified.

    Out of that you missed the #endif at the end of your header file