编译

源码编译教程

https://zhuanlan.zhihu.com/p/107516361

一次 Build 流程

  1. 在 VS 中点击 Build,调用 build.bat
  2. build.bat 中调用 UBT
  3. UBT 执行 target.cs 和所有 Module 的 build.cs 中的逻辑
  4. UBT 调用 UHT(根据 UE 的 宏标记 生成代码)
  5. UHT 生成完毕后,UBT 调用编译器
  6. 预处理
  7. 编译
  8. 链接

参考: https://ue5wiki.com/wiki/6362/#VS

Unity Build

UE 默认是开 Unity Build 的,在 Intermediate 里面,能找到对应的 Module.xxx.cpp (xxx为当前模块名称) 文件,里面整合了所有的 .gen.cpp 和 .cpp

检测到某个 cpp 更改时,会把这个 cpp 文件从 Module.xxx.cpp 中摘出去,这样其余文件还是可以 unity build,而我们在 ide 更改的这个 cpp 再单独 build

可以在模块的 .build.cs 中加代码 bUseUnity = false; 来关闭 Unity Build.

参考: https://ue5wiki.com/wiki/36076/

PCH

我们在 https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Programs/UnrealBuildTool/Configuration/ModuleRules.cs 中可以看到有这些 PCHUsageMode:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <summary>
/// What type of PCH to use for this module.
/// </summary>
public enum PCHUsageMode
{
    /// <summary>
    /// Default: Engine modules use shared PCHs, game modules do not
    /// </summary>
    Default,

    /// <summary>
    /// Never use any PCHs.
    /// </summary>
    NoPCHs,

    /// <summary>
    /// Never use shared PCHs.  Always generate a unique PCH for this module if appropriate
    /// </summary>
    NoSharedPCHs,

    /// <summary>
    /// Shared PCHs are OK!
    /// </summary>
    UseSharedPCHs,

    /// <summary>
    /// Shared PCHs may be used if an explicit private PCH is not set through PrivatePCHHeaderFile. In either case, none of the source files manually include a module PCH, and should include a matching header instead.
    /// </summary>
    UseExplicitOrSharedPCHs,
}

我们的代码文件,在 Intermediate 中都有个 .cpp.dep.json 文件,里头会有用到的 PCH 是什么。这个文件在 UBT 流程用到。

输出到编译器的参数选项可以在同级目录的 .cpp.obj.rsp 查看,可以看到上面的 .cpp.dep.json 就是作为 /sourceDependencies 参数输给 msvc 的( https://learn.microsoft.com/en-us/cpp/build/reference/sourcedependencies?view=msvc-170

Precompile

模块有两个属性 bUsePrecompiled 和 bPrecompile

bPrecompile 我还不清楚是什么用,bUsePrecompiled 默认则是关闭,打开的话则会可以直接用 Binaries 里面的二进制内容

因此对于不希望分发源码的情况,我们可以只保留二进制文件,并在 .build.cs 中添加:

1
bUsePrecompiled = !File.Exists(Path.Combine(PluginDirectory, "Source/xxx/xxx.cpp"));

这样,在有某个代码文件 xxx.cpp 存在时,bUsePrecompiled 为 false,走本地编译;在这个文件不存在时,bUsePrecompiled 为 true,直接用 Binaries 的内容。

参考: https://ue5wiki.com/wiki/16643/#bPreCompile%20%E4%B8%8E%20bUsePreCompiled

构建问题合集

1. UECommon.props(15,3): Error MSB4019 : 找不到导入的项目“E:\Program Files\JetBrains\JetBrains Rider 2023.2.3\tools\MSBuild\Microsoft\VC\v170\Microsoft.Cpp.Default.props”

https://ganzhixiong.com/p/cf1fabff/

2. error c4668 : ‘__has_feature’ is not defined as a preprocessor macro, replacing with ‘0’ for ‘#if/#elif’

一些奇怪的编译报错,先确定 vs 版本有没有装对,用 GenerateProjectFiles.bat 生成工程,看提示有没有推荐的版本,装那个版本。

参考

https://ue5wiki.com/wiki/6362/#VS

https://ue5wiki.com/wiki/16643/#bUseRTTI-bool

https://ue5wiki.com/wiki/36076/

Licensed under CC BY-NC-SA 4.0