MetaEditor HelpCreating ProgramsCompiling 

Compiling

Compilation is the process of translating the source code of a MQL5 program into the machine language. This process consists of several stages:

  • Lexical scan;
  • Parsing;
  • Semantic analysis;
  • Code generation;
  • Code optimization.

A successful compilation results in the creation of an executable file (*.EX5) of a program that can be started in the client terminal.

  • Any file (*.MQ5 or *.MQH) can be compiled, but an executable file (*.EX5) can be generated only as a result of the compilation of the main MQ5 file of a program.
  • An executable file is created in its own closed format which hides the source algorithm of a program.
  • Compiled executable EX5 files can be distributed without source MQ5 or MQH files. Debugging is impossible without them. It is recommended not to distribute EX5 files obtained in the result of debugging.

In order to start compilation, open the source file through the "Navigator" window and execute the "Compile Compile" command in the "File" menu or in the "Standard" toolbar, or you can press the "F7" key.

The report of the compilation process will be shown in the "Errors" tab of the "Toolbox" window. If there are no errors in this tab, you can start the program in the client terminal. Error and warning messages are marked by special icons near their descriptions:

Errors

Compilation errors are marked by Error icons both in the "Errors" tab and in the code. If such errors occur, an executable program file (*.EX5) won't be generated. In order to move to the line with the error, click twice with the left mouse-button on the line, or execute the "Go to line Go to line" command in the context menu. The number of the line and column, where an error has been found, is shown in corresponding columns.

The full description of compilation errors is given in the Help topics to the MQL5 language.

The appearance of alerts denoted by the Warning icon point to places where errors can occur. I.e. the compiler points to parts of a source code, that could be incorrectly interpreted (for example, implicit change of a value type). You can go to such code lines the same as to lines with errors.

It is possible that you do not process warnings. But it is not recommended, because they indicate potential places where errors can occur.

Besides, various informational messages are shown in the journal. They are marked by icons Information. They can show, for example, include files, to which a program referred during compilation.

An executable file (*.EX5) generated after a successful compilation, is located in the same place, where the main file of the program source code (*.MQ5) is located.

Compilation from the Command Line

The client terminal includes the compiler of MQL5 programs that runs separately from MetaEditor — MQL5.exe. It is located in the terminal installation folder.

The compiler can be started from any folder. The only requirement for starting it is having MQL5.dll (or its 64-bit version) in the same directory.

You are free to distribute the MQL5.exe and MQL5.dll files. However, you are not allowed to make any changes, disassemble or decompile these files as well as any MQL5 program objects.

The compiler is started from the command line, specifying the path and name of the file that you want to compile.

Example of running the compiler:

C:\Compiler\>mql5.exe C:\MyPrograms\script.mq5

The file has two run keys:

  • /s — If you run the compiler with this key, it will only check the program code syntax and shows respective results;
  • /i :[path] — with this key, you can specify the path to the /MQL5 directory of the client terminal. This parameter is required, for programs that often use additional include files (*.mqh).

Example of running the compiler with the additional key:

C:\Compiler\>mql5.exe C:\MyPrograms\script.mq5 /i:C:\client_terminal\MQL5

 

Styler
Debugging

MetaTrader 5 — a complex trading platform for providing brokerage services on the retail Forex market.

Copyright 2000-2012, MetaQuotes Software Corp.