如何定义用于包含文件的C ++宏

时间:2021-11-01 19:49:54

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.

我需要在我创建的每个.cpp中包含一个文件作为虚幻引擎项目的第一行......这不能包含在解决方案/项目中的每个cpp中,只包括我想要它的那些。

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?

虚幻引擎声明它必须是第一个包含。有没有一种方法可以对此进行宏观化,以便我只在一个地方定义它并使用每个cpp文件顶部的宏...这里几乎就像一个包含txt文件,这样编译器认为头部声明实际上位于顶部?

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"

...

2 个解决方案

#1


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:

在您的Makefile或其他方面,使用预先定义到您想要的路径的INCLUDE_CONFIG调用您的编译器;例如对于gcc,旗帜将是:

-DINCLUDE_CONFIG="./../../../Public/TemplatePlatformer.h"

#2


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.

一个简单而灵活的解决方案是创建单独的自定义头文件,其中包含此包含以及在每个.cpp开头需要完成的任何其他操作,然后包含该文件。

So you'd create a global.h containing

所以你要创建一个包含的global.h

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

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

然后在每个.cpp中你首先包括:

#include "global.h"

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

它比基于宏的解决方案更清晰,而且不是更多的打字。

#1


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:

在您的Makefile或其他方面,使用预先定义到您想要的路径的INCLUDE_CONFIG调用您的编译器;例如对于gcc,旗帜将是:

-DINCLUDE_CONFIG="./../../../Public/TemplatePlatformer.h"

#2


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.

一个简单而灵活的解决方案是创建单独的自定义头文件,其中包含此包含以及在每个.cpp开头需要完成的任何其他操作,然后包含该文件。

So you'd create a global.h containing

所以你要创建一个包含的global.h

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

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

然后在每个.cpp中你首先包括:

#include "global.h"

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

它比基于宏的解决方案更清晰,而且不是更多的打字。