---
title: "编译 - 开发程序 - MetaEditor 帮助"
description: "编译意即将 MQL4/MQL5 程序的源代码转换为机器语言。 结果是可以在交易平台上启动的可执行程序文件 (*.EX4 或 *.EX5)。 编译由几个阶段组成: 词汇分析 语法分析 语义分析 代码生成 代码优化 若要得到可执行文件，通过 导航器 打开... - 编译 - 开发程序 - MetaEditor 帮助"
image: "https://www.metatrader5.com/i/logo_metatrader5.png"
url: "https://www.metatrader5.com/zh/metaeditor/help/development/compile"
---

[MetaEditor 帮助](https://www.metatrader5.com/zh/metaeditor/help) → [开发程序](https://www.metatrader5.com/zh/metaeditor/help/development) → 编译

# 编译

编译意即将 MQL4/MQL5 程序的源代码转换为机器语言。 结果是可以在交易平台上启动的可执行程序文件 (*.EX4 或 *.EX5)。

编译由几个阶段组成:

- 词汇分析
- 语法分析
- 语义分析
- 代码生成
- 代码优化

> 任何文件 (*.MQ4, *.MQ5 或 *.MQH) 均可被编译, 但可执行文件 (*.EX4 或 *.EX5) 只能通过编译 [主体的](https://www.metatrader5.com/zh/metaeditor/help/structure#main_file) MQ4/MQ5 程序文件或 [项目](https://www.metatrader5.com/zh/metaeditor/help/mql5storage/projects) 才能得到。可执行文件以其自己的格式创建，隐藏原始程序算法。已编译的执行 EX4/EX5 文件可以在没有 MQ4、MQ5 和 MQH 源文件的情况下分发。 没有它们，调试是不可能的。 也不建议分发调试过程中得到的可执行 EX4/EX5 文件。

若要得到可执行文件，通过 [导航器](https://www.metatrader5.com/zh/metaeditor/help/workspace/navigator) 打开 [主体](https://www.metatrader5.com/zh/metaeditor/help/structure#main_file) 源文件或项目  并点击 ![编译](https://www.metatrader5.com/i/help/metaeditor/en/compile_icon.png) 编译在 [文件](https://www.metatrader5.com/zh/metaeditor/help/main_menu/main_menu_file#compile) 菜单里或按 F7。 编译过程显示在工具箱窗口的 [错误](https://www.metatrader5.com/zh/metaeditor/help/workspace/toolbox#errors) 选项卡上。 如果编译通过没有错误，您可以在交易平台上运行获得的程序。

![编译程序并在 &quot;错误&quot; 选项卡上显示结果](https://www.metatrader5.com/i/help/metaeditor/en/compilation.png)

如果有错误或警告，它们将显示在错误选项卡上。

编译错误标有![错误](https://www.metatrader5.com/i/help/metaeditor/en/error_icon.png)在错误选项卡上以及相应的代码段中。 如果这种情况发生，则不会创建可执行程序文件 (*.EX4 或 *.EX5)。 若要跳转到错误字符串，请双击错误或单击![跳转到行](https://www.metatrader5.com/i/help/metaeditor/en/go_to_line_icon.png)在关联菜单中跳转到行命令。 字符串和检测到错误的列将显示在相应的列中。

警告标有![警告](https://www.metatrader5.com/i/help/metaeditor/en/warning_icon.png)图标表示潜在错误的地方。 这些是可能被误解的源代码段 (例如，隐性数值类型变更)。 您可以像错误一样跳转到这些代码字符串。

![信息](https://www.metatrader5.com/i/help/metaeditor/en/info_icon.png)图标代表各种信息消息，例如，关于在编译过程中访问的头文件的消息。

## 编译模式 [#](#mode)

编辑器提供两种编译模式：包含最大限度的代码优化和没有优化。

当优化功能被禁用时，创建可执行文件所需的时间会明显减少。当您需要快速检查编写代码时，请在开发阶段使用此模式。然后，在最终程序编译期间，打开最大优化模式以获得最佳性能。

要在这些模式之间切换，可以使用 "构建 "菜单或工具栏上的编译命令菜单。

![控制编译模式](https://www.metatrader5.com/i/help/metaeditor/en/compilation_mode.png)

项目设置中的“[最大优化](https://www.metatrader5.com/zh/metaeditor/help/mql5storage/projects#properties)”参数执行相同的功能。

> 完整的编译错误描述已在 MQL4/MQL5 语言帮助中提供。在出现警告的情况下，依然会生成可执行文件。 但不应忽略警告，因为它们表明潜在的代码错误。在编译期间, 可执行文件 (*.EX4 或 *.EX5) 与程序的 [主体源文件](https://www.metatrader5.com/zh/metaeditor/help/structure#main_file) (*.MQ4 或 *.MQ5) 亦或 [项目](https://www.metatrader5.com/zh/metaeditor/help/mql5storage/projects) 生成在同一目录下。metaeditor.exe 可用作 [第三方集成开发环境的外部编译器](https://www.metatrader5.com/zh/metaeditor/help/beginning/integration_ide)。

## 针对不同架构的处理器进行编译 [#](#architecture)

现代处理器提供了一组可明显加快数学计算速度的高级指令：[AVX](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)、[AVX2](https://en.wikipedia.org/wiki/Advanced_Vector_Extensions)、[AVX512](https://en.wikipedia.org/wiki/AVX-512)和[FMA3](https://en.wikipedia.org/wiki/FMA_instruction_set)。这些指令在MQL5编译器中得到支持，可以生成更高效、更快速的代码。可以选择使用这些指令：

- 如果您正在为自己创建一个应用程序，并且确定它将在支持所需架构的处理器上运行，则可以使用高级指令对其进行编译。这将提供更高的性能。
- 如果您要创建供公共使用或出售的程序，请使用X64常规模式下的基本指令集对其进行编译。这将确保应用程序与用户硬件的最大程度兼容性。

要了解您的处理器支持哪些指令，请使用[CPU-Z免费实用程序](https://www.cpuid.com/softwares/cpu-z.html)。安装后，打开“CPU”部分并检查“指令”字段：

![使用CPU-Z检查您的处理器支持哪些指令](https://www.metatrader5.com/i/help/metaeditor/en/cpu_z.png)

要选择处理器架构，请使用编译菜单。接下来，点击“编译”。您还可以在[MetaEditor设置](https://www.metatrader5.com/zh/metaeditor/help/beginning/settings#compiler)和[单独的项目设置](https://www.metatrader5.com/zh/metaeditor/help/mql5storage/projects#properties)中指定架构。

![选择将为其编译程序的处理器架构](https://www.metatrader5.com/i/help/metaeditor/en/compilation_architecture.png)

如果是为了用户处理器不支持的架构编译应用程序，则尝试运行该程序将导致在平台日志中记录以下消息：

```
您的CPU架构不允许运行文件'<file-name>.ex5'：需要AVX512，您只有AVX2
```

限制：

- [MQL5云网络](https://www.metatrader5.com/zh/terminal/help/algotrading/strategy_optimization#cloud)和[内置VPS](https://www.metatrader5.com/zh/terminal/help/virtual_hosting)尚不支持AVX512处理器编译的程序。
- 只有在X64常规模式下编译的文件才能上传到[市场](https://www.mql5.com/zh/articles/385)。这是确保应用程序与用户硬件最大兼容性所必需的。

## In this section

- [智能管理](https://www.metatrader5.com/zh/metaeditor/help/development/intelligent_management)
- [查找和替换](https://www.metatrader5.com/zh/metaeditor/help/development/source_code_find)
- [样板](https://www.metatrader5.com/zh/metaeditor/help/development/styler)
- 编译
- [MQL5 云端保护: 为程序提供先进的保护](https://www.metatrader5.com/zh/metaeditor/help/development/mql5_cloud_protector)
- [代码调试](https://www.metatrader5.com/zh/metaeditor/help/development/debug)
- [代码分析](https://www.metatrader5.com/zh/metaeditor/help/development/profiling)
- [AI Assistant编码助手](https://www.metatrader5.com/zh/metaeditor/help/development/ai_assistant)
- [生成包含代码](https://www.metatrader5.com/zh/metaeditor/help/development/generate_mqh)
- [调用 C++ 的 DLL (与 MS Visual Studio 集成)](https://www.metatrader5.com/zh/metaeditor/help/development/c_dll)
- [使用Python](https://www.metatrader5.com/zh/metaeditor/help/development/python)
- [OpenCL 支持](https://www.metatrader5.com/zh/metaeditor/help/development/opencl)

```json
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [ {"@type": "ListItem", "position": 1, "item": {"@id": "https://www.metatrader5.com/zh/metaeditor/help", "name": "MetaEditor 帮助"}}, {"@type": "ListItem", "position": 2, "item": {"@id": "https://www.metatrader5.com/zh/metaeditor/help/development", "name": "开发程序"}}, {"@type": "ListItem", "position": 3, "item": {"@id": "https://www.metatrader5.com/zh/metaeditor/help/development/compile", "name": "编译"}} ]}
{ "@context": "https://schema.org", "@type": "FAQPage", "mainEntity": [{ "@type": "Question", "name": "MetaTrader 5: 编译", "acceptedAnswer": { "@type": "Answer", "text": "编译意即将 MQL4/MQL5 程序的源代码转换为机器语言。 结果是可以在交易平台上启动的可执行程序文件 (*.EX4 或 *.EX5)。 编译由几个阶段组成: 词汇分析 语法分析 语义分析 代码生成 代码优化 若要得到可执行文件，通过 导航器 打开 主体 源文件或项目 并点击 编译在 文件..." } }] }
```
