构建说明(Windows)
🌐 Build Instructions (Windows)
请按照以下指南在 Windows 上构建 Electron 本身,以便创建自定义的 Electron 二进制文件。有关将应用代码与预构建的 Electron 二进制文件打包和分发的内容,请参阅 应用分发 指南。
🌐 Follow the guidelines below for building Electron itself on Windows, for the purposes of creating custom Electron binaries. For bundling and distributing your app code with the prebuilt Electron binaries, see the application distribution guide.
先决条件
🌐 Prerequisites
- Windows 10 / Server 2012 R2 或更高版本
- 需要使用 Visual Studio 2019 (>=16.0.0) 构建,但推荐使用 Visual Studio 2022 (>=17.0.0) - 免费下载 VS 2022 社区版
- 有关所需的 Visual Studio 组件的详细信息,请参见 Chromium 构建文档。
- 如果你的 Visual Studio 安装在默认目录之外,你需要设置一些环境变量,以便将工具链指向你的安装路径。
vs2022_install = DRIVE:\path\to\Microsoft Visual Studio\2022\Community,将2022和Community替换为你安装的版本,并将DRIVE:替换为 Visual Studio 所在的驱动器。通常,这将是C:。WINDOWSSDKDIR = DRIVE:\path\to\Windows Kits\10,将DRIVE:替换为 Windows Kits 所在的驱动器。通常,这将是C:。
- Node.js >= 22.12.0
- Git
- 如果你打算创建完整的分发版,需要使用 Windows SDK 10.0.15063.468 的 Windows 调试工具,因为
symstore.exe用于从.pdb文件创建符号存储。- SDK 的不同版本可以并行安装。要安装 SDK,请打开 Visual Studio 安装程序,选择
Modify→Individual Components,向下滚动并选择要安装的合适的 Windows SDK。另一种选择是查看 Windows SDK 和模拟器存档,并分别下载独立版本的 SDK。 - 还必须安装 SDK 调试工具。如果通过 Visual Studio 安装程序安装了 Windows 10 SDK,那么可以通过以下路径安装:
Control Panel→Programs→Programs and Features→ 选择“Windows 软件开发工具包” →Change→Change→ 勾选“Windows 调试工具” →Change。或者,你也可以下载独立的 SDK 安装程序来安装调试工具。
- SDK 的不同版本可以并行安装。要安装 SDK,请打开 Visual Studio 安装程序,选择
如果你当前没有 Windows 安装, developer.microsoft.com 提供了带有时间限制的 Windows 版本,你可以用它们来构建 Electron。
🌐 If you don't currently have a Windows installation, developer.microsoft.com has timebombed versions of Windows that you can use to build Electron.
构建 Electron 完全依赖命令行脚本,无法通过 Visual Studio 完成。你可以使用任何编辑器开发 Electron,但将来会提供通过 Visual Studio 构建的支持。
🌐 Building Electron is done entirely with command-line scripts and cannot be done with Visual Studio. You can develop Electron with any editor but support for building with Visual Studio will come in the future.
即使不使用 Visual Studio 进行构建,它仍然是必需的,因为我们需要它提供的构建工具链。
从 Windows 安全性中排除源树
🌐 Exclude source tree from Windows Security
Windows 安全中心不喜欢 Chromium 源代码中的某个文件(见 https://crbug.com/441184),因此它会不断删除该文件,导致 gclient sync 问题。你可以按照这些说明将源代码目录排除在 Windows 安全监控之外。
🌐 Windows Security doesn't like one of the files in the Chromium source code
(see https://crbug.com/441184), so it will constantly delete it, causing gclient sync issues.
You can exclude the source tree from being monitored by Windows Security by
following these instructions.
架构
🌐 Building
请参阅 构建说明:GN
🌐 See Build Instructions: GN
32 位构建
🌐 32bit Build
要为32位目标构建,你需要将 target_cpu = "x86" 作为 GN 参数传入。你可以通过为 GN 使用不同的输出目录(例如 out/Release-x86)以及不同的参数来同时构建32位和64位目标。
🌐 To build for the 32bit target, you need to pass target_cpu = "x86" as a GN
arg. You can build the 32bit target alongside the 64bit target by using a
different output directory for GN, e.g. out/Release-x86, with different
arguments.
$ gn gen out/Release-x86 --args="import(\"//electron/build/args/release.gn\") target_cpu=\"x86\""
其他的搭建步骤完全一样。
🌐 The other building steps are exactly the same.
视觉工作室项目
🌐 Visual Studio project
要生成一个 Visual Studio 项目,你可以将 --ide=vs2017 参数传递给 gn gen:
🌐 To generate a Visual Studio project, you can pass the --ide=vs2017 parameter
to gn gen:
$ gn gen out/Testing --ide=vs2017
故障排除
🌐 Troubleshooting
未找到命令 xxxx
🌐 Command xxxx not found
如果你遇到类似 Command xxxx not found 的错误,可以尝试使用 VS2015 Command Prompt 控制台来执行构建脚本。
🌐 If you encountered an error like Command xxxx not found, you may try to use
the VS2015 Command Prompt console to execute the build scripts.
致命的内部编译器错误:C1001
🌐 Fatal internal compiler error: C1001
确保你安装了最新的 Visual Studio 更新。
🌐 Make sure you have the latest Visual Studio update installed.
LNK1181:无法打开输入文件 'kernel32.lib'
🌐 LNK1181: cannot open input file 'kernel32.lib'
尝试重新安装 32 位 Node.js。
🌐 Try reinstalling 32bit Node.js.
错误:ENOENT,无法获取 'C:\Users\USERNAME\AppData\Roaming\npm' 的状态
🌐 Error: ENOENT, stat 'C:\Users\USERNAME\AppData\Roaming\npm'
创建该目录应该可以解决问题:
🌐 Creating that directory should fix the problem:
$ mkdir ~\AppData\Roaming\npm
node-gyp 未被识别为内部或外部命令
🌐 node-gyp is not recognized as an internal or external command
如果你在使用 Git Bash 构建时可能会出现此错误,你应该改用 PowerShell 或 VS2015 命令提示符。
🌐 You may get this error if you are using Git Bash for building, you should use PowerShell or VS2015 Command Prompt instead.
无法在 '...' 创建目录:文件名太长
🌐 cannot create directory at '...': Filename too long
node.js 有一些 非常长的路径名,而默认情况下 Windows 上的 git 无法正确处理长路径名(尽管 Windows 支持它们)。这应该可以解决问题:
🌐 node.js has some extremely long pathnames, and by default git on windows doesn't handle long pathnames correctly (even though windows supports them). This should fix it:
$ git config --system core.longpaths true
错误:使用了未声明的标识符 'DefaultDelegateCheckMode'
🌐 error: use of undeclared identifier 'DefaultDelegateCheckMode'
这可能在构建期间发生,当调试工具已随 Windows 驱动工具包安装时。卸载 Windows 驱动工具包,然后按照上述步骤安装调试工具。
🌐 This can happen during build, when Debugging Tools for Windows has been installed with Windows Driver Kit. Uninstall Windows Driver Kit and install Debugging Tools with steps described above.
构建脚本挂起直至按键
🌐 Build Scripts Hang Until Keypress
这个漏洞是 Windows 命令提示符的“功能”。当启用 QuickEdit 时,在提示符窗口内点击会发生这种情况,其目的是便于选择和复制输出文本。由于每次意外点击都会暂停构建进程,你可能想在命令提示符属性中禁用此功能。
🌐 This bug is a "feature" of Windows' command prompt. It happens when clicking inside the prompt window with
QuickEdit enabled and is intended to allow selecting and copying output text easily.
Since each accidental click will pause the build process, you might want to disable this
feature in the command prompt properties.