Header files in C++ using more than once

Asked

Viewed 90 times

2

I am developing a cross-platform project (Windows - Linux - Macos). During its development and while generating documentation (via Doxygen) I realized that there are many files *.h to be called by several files *.cpp. My questions are as follows::

  1. It is possible to limit the amount of times that a header file is called in the project?
  2. Recommend the use of the directive #pragma once?
  3. Taking for example the file a.h includes a1.h and a2.h and is included by b.h and b.h needs a1.h, I have to call them implicitly into b.h? It is not enough to be already called in a.h?

1 answer

2


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

Yes, if the quantity is 1. More than that it would need auxiliary tools, but it makes no sense, there is no reason to enter 2 times.

They recommend the use of the "#pragma Once directive"?

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

Taking for example the file a.h includes a1.h and a2.h and is included by b.h and b.h needs a1.h, I have to call them implicitly into b.h? It is not enough to be already called in a.h?

No need. Under normal conditions it will already be included and this is until the reason to exist the pragma once. You could include to ensure and not risk entering twice.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.