0

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?

bruno
  • 32,421
  • 7
  • 25
  • 37
James Frezz
  • 39
  • 1
  • 10
  • 1
    if you compile 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. Out of that you missed the `#endif` at the end of your header file – bruno May 04 '20 at 19:30
  • Ok, I see... Is there any way to make it "parsed" only once or something like that? Will a precompiled header help? – James Frezz May 04 '20 at 19:33
  • 1
    not possible, there is a 'raz' each time a files is compiled, and hopefully – bruno May 04 '20 at 19:34
  • Out of curioisity - what is "raz"? – James Frezz May 04 '20 at 19:40
  • oh yes sorry, is (french) *Remise A Zero* == reset – bruno May 04 '20 at 19:44
  • Code included multiple times will get compiled multiple times. No surprise there. – Jesper Juhl May 04 '20 at 19:53

1 Answers1

1

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

bruno
  • 32,421
  • 7
  • 25
  • 37