0

Say I have the following:

Main.cpp

#include <Windows.h>
#include "B.h"

...

-

B.h

...
SomePrototypeFunctionNeedingWindowsH();

-

In B.h, I'm not required to include Windows.h again as it's already been included beforehand. For clarity, I would like to be required to include Windows.h for each new file that wants it. I'm using VS2015.

Can this be done?

Can this be done without impact on compilation time?

Would this be considered an acceptable practice?

Will I run in to any issues if this was done?

A. Voll
  • 63
  • 7
  • 1
    Why would you want to do that? – meneldal Dec 21 '15 at 01:56
  • This is what include-guards are for. And most compilers "understand" include guards and don't even open the file if it's been included before. – Mats Petersson Dec 21 '15 at 02:00
  • (I personally will include Windows.h inside B.h too, so that you don't have to always know that to use B.h, I need to include Windows.h) – Mats Petersson Dec 21 '15 at 02:01
  • Including windows.h in b.h is the normal way to go. Why would it be a good thing to include windows and b everywhere b is needed ? – m_pOatrix Dec 21 '15 at 02:02
  • I want to include inside B.h purely for semantic/readability reasons, not for compilation purposes. On further reflection, this could perhaps be quite problematic if there's long chains of header dependencies. – A. Voll Dec 21 '15 at 02:18
  • Then don't include `windows.h` in B.h so people will **have** to include it in other files – meneldal Dec 21 '15 at 02:48
  • Hmm, I may be wrong here. I "did" actually have to include Windows.h in B.h. VS seems to only give the error at compile time, not edit time. – A. Voll Dec 21 '15 at 03:21

2 Answers2

2

Maybe you're looking for the preprocessor directives in C++. They are something like:

#ifndef HEADERFILE_H
#define HEADERFILE_H
/*Your header declarations/definitions*/
#endif

In this preprocessor technique, you basically tell your compiler that it should not include the same header for multiple times. Refer to this post for more thorough understanding

Community
  • 1
  • 1
Syed Ali Hamza
  • 193
  • 1
  • 10
  • Not quite, I don't want the header to be actually included as that would cause problems. I just want the include there as a form of documentation that tells me what is being used in the file. It also allows the file to stand on it's own more if you wanted to distribute it, instead of having to send every other file you have over too. – A. Voll Dec 21 '15 at 02:20
0

Compile each .h file into throw-away test output .objs. This can be done manually, or through a script to run on whatever project management system you use.

Naturally something has to add the .h files to the project management system.

A .h file that can be compiled as a source file includes all the header files it needs.

The exact steps -- you could iterate through each .h inmthe directory tree, output a .h.cpp file, add that .h.cpp file to a project that you do not otherwise use, and build that project.

What language ypu write this in depends on what scripting languages you are good at.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • That's a clever way to think about it. It's not an extent I'm willing to go to yet, it's just a feature I would have liked, but is by no means important. I will keep this in mind, thanks. – A. Voll Dec 21 '15 at 03:14