Header files in C ++ using more than once

2

I'm developing a multiplatform project (Windows - Linux - MacOS). During its development and when generating documentation (via Doxygen) I realized that there are many *.h files to be called by several *.cpp files. My questions are as follows:

  • Is it possible to limit the number of times a header file is called in the project?
  • Do you recommend using the #pragma once directive?
  • For example, the a.h file includes a1.h and a2.h and is included by b.h and b.h needs a1.h , do I have to implicitly call them b.h ? Is not it enough that they are already called in a.h ?
  • asked by anonymous 04.07.2018 / 17:57

    1 answer

    2
      

    Is it possible to limit the number of times a header file is called in the project?

    Yes, if the quantity is 1. More than that you would need auxiliary tools, but it does not make sense, you do not have to enter 2 times.

      

    Do you recommend using the "#pragma once" directive?

    In the compilers that accept , yes, I prefer its use. Not everyone accepts and prefers other mechanisms that do the same. I think preciosismo in the present stage. In general, true C or C ++ programmers are above average, but I see many having these things to follow cake recipe without looking at the context.

      

    For example, the file a.h includes a1.h and a2.h and is included by b.h and b.h needs a1.h , do I have to implicitly call them b.h ? Is not it enough that they are already called in a.h ?

    No need. Under normal conditions it will already be included and this is even the reason why pragma once exists. You could include it to ensure you do not risk going in twice.

    You can do some nonsense and make the second entry be processed differently, but I do not recommend this, even if it has some use.

        
    04.07.2018 / 18:04