Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

為 Windows 進行編譯

也參考

這個頁面描述的是如何從源碼編譯 Windows 編輯器和匯出範本二進位檔案。如果你要找的是匯出專案到 Windows,請閱讀 為 Windows 匯出

系統需求

若要在 Windows 下進行編譯,需要滿足下列條件:

  • Visual Studio Community 2017 版或更新版本。建議使用 VS 2019。 請務必閱讀下方的「Visual Studio 安裝須知」,否則會需要重新執行或下載安裝程式。

  • 配有 GCC 的 MinGW-w64 可用來代替 Visual Studio。請確定有以 posix 執行緒模型來進行安裝或設定。

  • Python 3.5+請確保在安裝程式中勾選將 Python 新增到 ``PATH`` 的選項。

  • SCons 建構系統。推薦使用最新版本,尤其是為了支援最近的 Visual Studio 版本。

備註

若有安裝 ``Scoop <https://scoop.sh/>`_ ,則可以通過下列指令來輕鬆安裝 MinGW 以及其他相依性套件:

scoop install gcc python scons make mingw

備註

若有安裝 MSYS2 ,則可以通過下列指令輕鬆安裝 MinGW 以及其他相依性套件:

pacman -S mingw-w64-x86_64-python3-pip mingw-w64-x86_64-gcc  \
    mingw-w64-i686-python3-pip mingw-w64-i686-gcc make

必須於各個 MSYS2 MinGW 子系統的 Shell 中執行 pip install scons

也參考

要獲取編譯所需的 Godot 源碼,請參閱 取得原始碼

有關配合 Godot 使用 SCons 的一般說明,請參考 建置系統簡介

設定 SCons

若要安裝 SCons,請開啟命令提示字元並輸入下列指令。

python -m pip install scons

如果你看到提示資訊 Defaulting to user installation because normal site-packages is not writeable,那麼你就需要提升許可權後再重新執行該命令。使用管理員身份打開一個新的命令提示符,然後再執行該命令,確保 SCons 可以通過 PATH 存取。

若要檢查是否有正確安裝 Python 與 SCons,可以在命令提示字元 (cmd.exe) 中輸入 python --versionscons --version

若上述指令執行失敗,請確定有在安裝 Python 後將 Python 加至 PATH 環境變數中,然後再試一次。要將 Python 加入 PATH 環境變數,可以再次執行 Python 安裝程式,並開啟將 Python 加至 PATH 的選項。

如果 SCons 無法偵測到你的 Visual Studio 安裝,可能是因為你的 SCons 版本太老了。請使用 python -m pip install --upgrade scons 將其更新為最新版本。

Visual Studio 安裝須知

若安裝的 Visual Studio 是 2017 或 2019,請確定有在工作流程列表中開啟 C++

若安裝的 Visual Studio 是 2015,請不要使用 自定 安裝,請選擇 自定 安裝,然後選擇語言為 C++

若已經安裝好 Visual Studio 卻沒有開啟 C++ 支援的話,請重新執行安裝程式。重新執行安裝程式時應該有一個 修改 按鈕。若從 新增或移除程式 中執行安裝程式則只會出現 修復 選項,修復選項無法用於安裝 C++ 工具。

下載 Godot 原始碼

更多資訊請參考 匯出專案

本教學假設原始碼放置於 C:\godot 中。

警告

為了防止編譯過程中由於連續病毒掃描而導致的速度減慢,請將 Godot 原始檔案夾新增到殺毒軟體中的例外列表中。

For Windows Defender, hit the Windows key, type "Windows Security" then hit Enter. Click on Virus & threat protection on the left panel. Under Virus & threat protection settings click on Mange Settings and scroll down to Exclusions. Click Add or remove exclusions then add the Godot source folder.

編譯

選擇編譯器

SCons 會自動找到並使用現有的 Visual Studio 安裝。若尚未安裝 Visual Studio,則 SCons 會試著使用 MinGW 來代替。若有安裝 Visual Studio,但想使用 MinGW 的話,可以在命令行中將 use_mingw=yes 傳給 SCons。

在開發時,最好選擇使用 Visual Studio 編譯器,因為 Visual Studio 編譯器連結到 Godot 二進位檔的速度比 MinGW 快多了。但,MinGW 通過使用連結時期最佳化 (詳見下方),能產生更加最佳化的二進位檔,因此對於正式使用,MinGW 比起 Visual Studio 編譯器來得更好。

執行 SCons

開啟命令提示字元後,請 (使用 cd) 將目錄更改至 Godot 原始碼的根目錄,然後輸入:

C:\godot> scons platform=windows

備註

當使用多個 CPU 執行緒進行編譯,SCons 可能會提示缺少 pywin32。可以安心地忽略這個警告。

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

該執行檔包含了完整的 Godot 遊戲引擎,並且不需任何相依性套件即可執行。執行這個檔案會開啟專案管理員。

備註

若是要為正式環境編譯 Godot,則可以通過加上 target=release_debug SCons 選項來讓最終產生的可執行檔更小且更快速。

若使用 MinGW 來編譯 Godot,還可以通過 use_lto=yes 選項來讓產生的二進位檔案更小更快。由於連結時期最佳化是一項消耗記憶體的過程,因此在編譯時會需要至少 3GB 的可用記憶體。

備註

若想在客製化建置上使用與 Godot 官方釋出版本不同的編輯器設定,可以通過在 bin/ 資料夾中建立檔名為 ._sc__sc_ 的檔案來開啟 自包含模式

使用 Visual Studio 或其他 IDE 來進行開發

編譯 Godot 不需要使用 IDE,SCons 會處理所有事情。但是如果你想要做引擎開發,或者除錯引擎的 C++ 程式碼,你可能會對配置程式碼編輯器或者 IDE 感興趣。

基於資料夾的編輯器不需要任何特殊的設定就可以開始使用 Godot 的程式碼庫。要使用 Visual Studio 來編輯專案,需要先設定解決方案。

可以通過 SCons 來建立 Visual Studio 解決方案。要建立解決方案,請使用 vsproj=yes 參數來執行 SCons,如:

scons p=windows vsproj=yes

現在可以在 Visual Studio 解決方案中開啟 Godot 原始碼了,然後可以通過 Visual Studio 的 建置 按鈕來建置 Godot。

也參考

SCons 3.0+ (詳情請參考 為 Windows 進行編譯)。

從其他作業系統上為 Windows 進行交叉編譯

若你使用 Linux 或 macOS,則需要安裝 MinGW-w64 。MinGW-w64 通常會有 32 位元與 64 位元的版本。依據發行版不同,可能會有不同的套件名稱。下列為一些已知的套件名稱:

Arch Linux

pacman -Sy mingw-w64

Debian / Ubuntu

apt install mingw-w64

Fedora

dnf install mingw64-gcc-c++ mingw64-winpthreads-static \
            mingw32-gcc-c++ mingw32-winpthreads-static

macOS

brew install mingw-w64

Mageia

urpmi mingw64-gcc-c++ mingw64-winpthreads-static \
      mingw32-gcc-c++ mingw32-winpthreads-static

在嘗試編譯前,SCons 會在 PATH 環境變數中檢查下列二進位檔:

i686-w64-mingw32-gcc
x86_64-w64-mingw32-gcc

If the binaries are not located in the PATH (e.g. /usr/bin), you can define the following environment variable to give a hint to the build system:

export MINGW_PREFIX="/path/to/mingw"

Where /path/to/mingw is the path containing the bin directory where i686-w64-mingw32-gcc and x86_64-w64-mingw32-gcc are located (e.g. /opt/mingw-w64 if the binaries are located in /opt/mingw-w64/bin).

若要確定是否有正確設定,可以通過在 Shell 中執行下列指令來確認是否有可使用的編譯器 (輸出的版本可能會依據不同系統而有所不同):

${MINGW_PREFIX}/bin/x86_64-w64-mingw32-gcc --version
# x86_64-w64-mingw32-gcc (GCC) 13.2.0

備註

When cross-compiling for Windows using MinGW-w64, keep in mind only x86_64 and x86_32 architectures are supported. Be sure to specify the right arch= option when invoking SCons if building from a different architecture.

疑難排解

在某些 Ubuntu 版本中進行交叉編譯可能會出現 這個 Bug ,這是因為預設的設定未包含 POSIX 執行緒支援。

可以通過如下說明來更改此一設定。64 位元:

sudo update-alternatives --config x86_64-w64-mingw32-gcc
<choose x86_64-w64-mingw32-gcc-posix from the list>
sudo update-alternatives --config x86_64-w64-mingw32-g++
<choose x86_64-w64-mingw32-g++-posix from the list>

32 位元:

sudo update-alternatives --config i686-w64-mingw32-gcc
<choose i686-w64-mingw32-gcc-posix from the list>
sudo update-alternatives --config i686-w64-mingw32-g++
<choose i686-w64-mingw32-g++-posix from the list>

建立 Windows 匯出樣板

可以通過編譯不含編輯器的 Godot 來建立 Windows 匯出樣板。請使用下列旗標:

C:\godot> scons platform=windows target=template_debug arch=x86_32
C:\godot> scons platform=windows target=template_release arch=x86_32
C:\godot> scons platform=windows target=template_debug arch=x86_64
C:\godot> scons platform=windows target=template_release arch=x86_64

若之後有準備要取代標準匯出樣板,請將這些檔案複製到下列位置。請將其中的 <version> 改為版本識別項 (如 3.1.1.stable3.2.dev):

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

並改為下列名稱:

windows_32_debug.exe
windows_32_release.exe
windows_64_debug.exe
windows_64_release.exe

但是,如果使用自定模組或自定引擎原始碼,則可能會需要在此將自定二進位檔設為自定匯出樣板:

../../../_images/wintemplates.png

這種情況下則不需要複製檔案,只需要將產生的檔案參照進 Godot 原始碼資料夾的 bin\ 目錄中即可。這樣一來,下次進行建置時,便會自動參照自定樣板。