C++宏在预编译阶段会展开,UE4中又使用了大量的宏,阅读起来,十分的费劲。我相信大多数人在读懂这样代码时,都很吃力。要是看到宏展开后的中间文件,那该是多么每秒的事情。因此也就有了本篇的内容,获取预编译产生的中间文件。
在普通的VS工程中只需要简单的改一下配置就可以得到,但是由于UE4编译有UHT,UBT等的参入,项目的设置和普通工程大不一样。如下图
![772344d9a3dffc3abe95741904af880c.png](https://i-blog.csdnimg.cn/blog_migrate/7cd8900f590919fdcea3b69625ccf97c.jpeg)
左边是我通过VS创建的一个Console Project的属性,右边是UE4的Program的project属性。
既然没有了方便的配置,那么只能曲线救国。
准备工作:
下载引擎源码>Copy Programs/BlankProgram并重命名为PreprocessFileVisual
具体操作可以参考
UE4 Slate教程5--Slate独立程序blog.csdn.net![97332169f9fd1f3a0ac46e5dd7575958.png](https://i-blog.csdnimg.cn/blog_migrate/b00a51597eaf39bdb2b652c3836706e6.jpeg)
接下来的一步是关键:
打开PreprocessFileVisual.Target.cs做如下修改
文件public PreprocessFileVisualTarget(TargetInfo Target) : base(Target)
{
。。。
bPreprocessOnly = true;//为当前Target生成预编译文件
}
bPreprocessOnly 默认是False的。添加完毕之后,编译PreprocessFileVisual然后找到UnrealEngineEngineIntermediateBuildWin64FeatureLearningDebugPreprocessFileVisual目录在其中可以找到一个名为PreprocessFileVisual.cpp.i.response。这个文件就是我们生成.i文件的关键。
最后一步:
使用x64 Native Tools Command Prompt for VS 2019,我的vs是2019。
按下win搜索 native tools即可找到。运行之后定位到PreprocessFileVisual.cpp.i.response所在目录,运行cl.exe @PreprocessFileVisual.cpp.i.response
![bd130d0d6f62e9918d128d1555c1f70b.png](https://i-blog.csdnimg.cn/blog_migrate/8131c623262faba3cb325968cc387c86.png)
运行结果如图,此时就可以在UnrealEngineEngineIntermediateBuildWin64FeatureLearningDebugPreprocessFileVisual找到这个中间文件了。
PreprocessFileVisual.cpp.i
我在里面做了个测试,在Shader.h文件中有结构体
class FShaderParameterMapInfo
{
DECLARE_TYPE_LAYOUT(FShaderParameterMapInfo, NonVirtual);
public:
LAYOUT_FIELD(TMemoryImageArray<FShaderParameterInfo>, UniformBu