为Windows平台编译

需求

要在Windows下进行编译,需要以下内容:

  • Visual Studio Community, version 2017 or later. VS 2019 is recommended. Make sure to read “Installing Visual Studio caveats” below or you will have to run/download the installer again.
  • MinGW-w64 with GCC can be used as an alternative to Visual Studio. Be sure to install/configure it to use the posix thread model.
  • Python 3.5+.
  • SCons 3.0 build system. If using Visual Studio 2019, you need at least SCons 3.1.1.
  • 可选 - yasm (用于WebM SIMD优化)

注解

如果您已安装 Scoop,则可以使用以下命令轻松安装MinGW和其他依赖项:

  1. scoop install gcc python scons yasm make

注解

If you have MSYS2 installed, you can easily install MinGW and other dependencies using the following command:

  1. pacman -S mingw-w64-x86_64-python3-pip \
  2. mingw-w64-x86_64-gcc mingw-w64-x86_64-yasm \
  3. mingw-w64-i686-python3-pip mingw-w64-i686-gcc \
  4. mingw-w64-i686-yasm make

For each MSYS2 MinGW subsystem, you should then run pip install scons in its shell.

参见

有关Godot的SCons用法的一般概述,请参阅 构建系统介绍

Setting up Python

First you need to install Python 3.5 or newer. Make sure to enable the option to add Python to the PATH in the Python installer. The SCons installer should then detect and use the existing Python installation.

设置SCons

要安装SCons,请打开命令提示符并运行以下命令。

python -m pip install scons

要检查是否已正确安装Python和SCons,可以在命令提示符 (cmd.exe) 中键入 python --versionscons --version

如果上面的命令不起作用,请确保在安装后将Python添加到 PATH 环境变量中,然后再次检查。为此,您可以再次运行Python安装程序并启用将Python添加到 PATH 的选项。

安装Visual Studio注意事项

如果安装Visual Studio 2017或2019,请确保启用要安装的工作流程列表中的 C++

如果安装Visual Studio 2015,请确保运行 自定义 安装而不是 典型 安装,然后选择 C++ 作为语言。

如果您已经犯了在没有C++支持的情况下安装Visual Studio的错误,请再次运行安装程序;它将为您提供一个 修改 按钮。从 添加/删除程序 中运行安装程序只会给您提供 修复 选项,而该选项将不允许您安装C++工具。

下载Godot的源代码

Godot的源代码位于 GitHub 上。建议使用 Git 下载(克隆)。

从现在开始,本教程将假设您将源代码放在 C:\ godot

开始编译

选择编译器

SCons将自动查找并使用现有的Visual Studio安装。如果您没有安装Visual Studio,它将尝试改用MinGW。如果您已经安装了Visual Studio,并且想使用MinGW,请将 use_mingw=yes 传递给SCons命令行。

在开发过程中,通常最好使用Visual Studio编译器,因为它比MinGW更快地链接Godot二进制文件。但是,MinGW可以使用链接时间优化(请参见下文)来生成更多优化的二进制文件,从而使其成为生产使用的更好选择。

运行SCons

打开命令提示符后,转到引擎源代码的根目录(使用 cd),然后键入:

  1. C:\godot> scons platform=windows

您可以指定CPU线程数来加速构建:

  1. C:\godot> scons -j6 platform=windows

In general, it is OK to have at least as many threads compiling Godot as you have cores in your CPU, if not one or two more. Feel free to add the -j option to any SCons command you see below.

注解

使用多个CPU线程进行编译时,SCons可能会警告pywin32丢失。您可以放心地忽略此警告。

If all goes well, the resulting binary executable will be placed in C:\godot\bin\ with the name godot.windows.tools.32.exe or godot.windows.tools.64.exe. By default, SCons will build a binary matching your CPU architecture, but this can be overridden using bits=64 or bits=32.

该可执行文件包含整个引擎,并且运行时没有任何依赖关系。运行它会启动项目管理器。

注解

如果您正在编译Godot用于生产用途,那么您可以通过添加SCons选项 target=release_debug 来使最终的可执行文件更小更快。

如果要使用MinGW编译Godot,则可以通过添加SCons选项 use_lto=yes 使二进制文件变得更小、更快。由于链接时间优化是一个占用大量内存的过程,因此编译时将需要大约3 GB的可用RAM。

注解

If you want to use separate editor settings for your own Godot builds and official releases, you can enable Self-contained mode by creating a file called ._sc_ or _sc_ in the bin/ folder.

在Visual Studio或其他IDE中进行开发

对于大多数项目,只使用脚本就足够了,但是当需要C++开发时,为了创建模块或扩展引擎,通常需要使用IDE。

您可以通过使用 vsproj=yes 参数运行SCons来创建Visual Studio解决方案,如下所示:

  1. scons p=windows vsproj=yes

您现在可以在Visual Studio解决方案中打开Godot的源代码,并能够通过Visual Studio的 构建 按钮构建Godot。

如果需要编辑构建命令,它们位于“Godot”项目设置,NMAKE表中。在命令结束时调用SCons。如果出错,请从其他构建配置(debug,release_debug,release)或体系结构(Win32 / x64)中复制命令。它们是等价的。

从其他操作系统为Windows交叉编译

If you are a Linux or macOS user, you need to install MinGW-w64, which typically comes in 32-bit and 64-bit variants. The package names may differ based on your distribution, here are some known ones:

Arch Linux安装 AUR中的mingw-w64-gcc
Debian / Ubuntu
  1. apt install mingw-w64
Fedora
  1. dnf install mingw64-gcc-c++ mingw64-winpthreads-static \
  2. mingw32-gcc-c++ mingw32-winpthreads-static
macOS
  1. brew install mingw-w64
Mageia
  1. urpmi mingw64-gcc-c++ mingw64-winpthreads-static \
  2. mingw32-gcc-c++ mingw32-winpthreads-static

在尝试编译之前,SCons将在 PATH 环境变量中检查以下二进制文件:

  1. i686-w64-mingw32-gcc
  2. x86_64-w64-mingw32-gcc

如果这些二进制文件不在 PATH 中(例如 /usr/bin),则可以定义以下环境变量来提示以构建系统:

  1. export MINGW32_PREFIX="/path/to/i686-w64-mingw32-"
  2. export MINGW64_PREFIX="/path/to/x86_64-w64-mingw32-"

为了确保您做的事正确,在shell中执行以下操作将导致编译器正常工作(版本输出可能因您的系统而异):

  1. ${MINGW32_PREFIX}gcc --version
  2. # i686-w64-mingw32-gcc (GCC) 6.1.0 20160427 (Mageia MinGW 6.1.0-1.mga6)

故障排除

由于默认配置不支持POSIX线程,因此从某些Ubuntu版本进行交叉编译可能会导致 此bug

您可以按照以下说明针对64位更改该配置:

  1. sudo update-alternatives --config x86_64-w64-mingw32-gcc
  2. <choose x86_64-w64-mingw32-gcc-posix from the list>
  3. sudo update-alternatives --config x86_64-w64-mingw32-g++
  4. <choose x86_64-w64-mingw32-g++-posix from the list>

对于32位:

  1. sudo update-alternatives --config i686-w64-mingw32-gcc
  2. <choose i686-w64-mingw32-gcc-posix from the list>
  3. sudo update-alternatives --config i686-w64-mingw32-g++
  4. <choose i686-w64-mingw32-g++-posix from the list>

创建Windows导出模板

Windows导出模板是通过编译不使用编辑器的Godot来创建的,带有以下标志:

  1. C:\godot> scons platform=windows tools=no target=release_debug bits=32
  2. C:\godot> scons platform=windows tools=no target=release bits=32
  3. C:\godot> scons platform=windows tools=no target=release_debug bits=64
  4. C:\godot> scons platform=windows tools=no target=release bits=64

如果计划替换标准导出模板,复制模板到以下位置,并用版本标识符(例如 3.1.1.stable3.2.dev)替换 <version>

  1. %USERPROFILE%\AppData\Roaming\Godot\templates\<version>\

使用以下名称:

  1. windows_32_debug.exe
  2. windows_32_release.exe
  3. windows_64_debug.exe
  4. windows_64_release.exe

此外,如果您使用的是自定义模块或自定义引擎代码,则可能需要在此处将二进制文件配置为自定义导出模板:

../../_images/wintemplates.png

在这种情况下,您不需要复制它们,只需引用在Godot源文件夹的 bin\ 目录中生成的文件,因此下次构建时,将自动引用自定义模板。