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"

...
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • you can use [precompiled header](http://en.wikipedia.org/wiki/Precompiled_header). it is not designed for such purpose but basically it add some headers to all your cpp files – Bryan Chen Jan 28 '15 at 00:11
  • Depending on your compiler. Possible duplicate of http://stackoverflow.com/questions/3387453/include-header-files-from-command-line-gcc – inetknght Jan 28 '15 at 00:12
  • @BryanChen I'm not sure what that means? – Jimmyt1988 Jan 28 '15 at 00:12
  • 1
    @BryanChen precompiled headers are a different feature and AFAIK does *not* guarantee that a given header is included in every translation unit – inetknght Jan 28 '15 at 00:13
  • @inetknght I know it is different feature but I never know it doesn't guarantee given header is included? – Bryan Chen Jan 28 '15 at 00:20
  • What compiler and build system are you using? – Beta Jan 28 '15 at 00:28
  • I'm using Visual Studio 2014 Community edition - The answer is i don't know – Jimmyt1988 Jan 28 '15 at 00:29
  • If you were using gcc, you could do this without modifying the source files at all; I don't know whether that's possible in VS. – Beta Jan 28 '15 at 00:32

2 Answers2

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"
M.M
  • 138,810
  • 21
  • 208
  • 365
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.

sth
  • 222,467
  • 53
  • 283
  • 367
  • 3
    You could also add `../../../Public` to your include file search path and then just `#include ` everywhere, if that's what you are trying to accomplish. – sth Jan 28 '15 at 00:31
  • 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. – Jimmyt1988 Jan 28 '15 at 00:35
  • I ended up with: JIMMYT1988_CONFIG_INCLUDE="$(SolutionDir)Source\TemplatePlatformer\Public\TemplatePlatformer.h" – Jimmyt1988 Jan 28 '15 at 00:46