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?

7
  • 1
    Why would you want to do that? Commented Dec 21, 2015 at 1: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. Commented Dec 21, 2015 at 2: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) Commented Dec 21, 2015 at 2: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 ? Commented Dec 21, 2015 at 2:02
  • I want to include <Windows.h> 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. Commented Dec 21, 2015 at 2:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.