2

I need to include a file into every .cpp I have created as the first line in an Unreal Engine project... This cannot include it for EVERY cpp in the solution/project, just the ones I want it to.

That file is:

#include "./../../../Public/TemplatePlatformer.h"

Unreal Engine states that it has to be the first include. Is there a way of macroing this so that I just define it in one place and use the macro at the top of each cpp file...almost like an include txt file here so that the compiler thinks the header declaration is actually at the top?

this:

MyClass.cpp

#include "./../../../Public/TemplatePlatformer.h"
#include "MyClass.h"

...

MyClass2.cpp

#include "./../../../Public/TemplatePlatformer.h"
#include "MyClass2.h"

...

would become this:

MyClass.cpp

INCLUDE_CONFIG
#include "MyClass.h"

...

MyClass2.cpp

INCLUDE_CONFIG
#include "MyClass2.h"

...
8
  • you can use precompiled header. it is not designed for such purpose but basically it add some headers to all your cpp files Commented Jan 28, 2015 at 0:11
  • Depending on your compiler. Possible duplicate of stackoverflow.com/questions/3387453/… Commented Jan 28, 2015 at 0:12
  • @BryanChen I'm not sure what that means? Commented Jan 28, 2015 at 0:12
  • 1
    @BryanChen precompiled headers are a different feature and AFAIK does not guarantee that a given header is included in every translation unit Commented Jan 28, 2015 at 0:13
  • @inetknght I know it is different feature but I never know it doesn't guarantee given header is included? Commented Jan 28, 2015 at 0:20

2 Answers 2

4

You can write:

#include INCLUDE_CONFIG

and in your Makefile or otherwise, invoke your compiler with INCLUDE_CONFIG pre-defined to the path you want; e.g. for gcc the flag would be:

-DINCLUDE_CONFIG="./../../../Public/TemplatePlatformer.h"
Sign up to request clarification or add additional context in comments.

3 Comments

Do you know where i set this in visual Studio 2013?
thanks man, perfect... I stripped off the -D part and it worked great!
@Jimmyt1988 I don't use VS so can't say exactly, but there should be a section under Project Properties where you can specify conditional defines
2

A simple and more flexible solution would be to create separate custom header file that contains this include and whatever else you need to be done at the start of every .cpp, and then include that.

So you'd create a global.h containing

#include "./../../../Public/TemplatePlatformer.h"

and then in every .cpp you'd first include that:

#include "global.h"

It's much clearer and not really more typing than a macro based solution.

3 Comments

You could also add ../../../Public to your include file search path and then just #include <TemplatePlatformer.h> everywhere, if that's what you are trying to accomplish.
You totally win this, this is much more elegant. Thank you buddy! Although, it does seem to interfere with some of the other solution.. hmm.
I ended up with: JIMMYT1988_CONFIG_INCLUDE="$(SolutionDir)Source\TemplatePlatformer\Public\TemplatePlatformer.h"

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.