How to compile an MQL4 file with a command-line tool?

Donato Szilagyi picture Donato Szilagyi · Jan 26, 2013 · Viewed 11.9k times · Source

Now I am compiling my MetaTrader .mq4 files to .ex4 files with MetaEditor.

But my .mq4 files are generated by a Java-process, and I would like to automate the compilation process.

Is there a command-line compiler tool I could call programmatically?

Answer

kenorb picture kenorb · Sep 8, 2015

To compile a source code file from a command line, you can use MetaEditor for that. For example:

metaeditor.exe /compile:"C:\Program Files\Platform\MQL5\Scripts\myscript.mq5"

For 64-bit use metaeditor64.exe instead.

In Linux/macOS, this can be achieved using Wine, e.g.:

wine metaeditor.exe /compile:"MQL4/Experts/MACD Sample.mq4"

For mass compilation, you can specify folder, like:

metaeditor.exe" /compile:"MQL5\Scripts"

To specify custom MQL5/MQL4 folder with include files, you can use /inc parameter, for example:

metaeditor.exe /compile:"./Scripts" /inc:"C:\Program Files\TradingPlatform 2\MQL5"

For additional information about the compilation process, you can use /log:

metaeditor.exe /compile:"C:\Program Files\Platform\MQL5\Scripts\myscript.mq5" /log 

To check for the syntax only, add extra /s.

If the compilation fails, the MQL4.log file would be created in the platform folder with the relevant details. It's going to be in UTF-16 format, so you may need a special tool for it (such as Vim, Ruby, findstr or rg).

To specify the custom compilation log file, use /log:file.log parameter, e.g.

metaeditor.exe /log:errors.log /compile:.

Note: Display to the standard output is not supported (although on Linux you can use: /log:CON).

For more information, check: Compilation from the Command Line


Some time ago you could download the compiler of MQL4/MQL5 programs that runs separately from MetaEditor — MQL.exe. It was distributed separately from the terminal and you could download it at the following addresses:

https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mql.exe

https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mql64.exe

Usage (as per MQL4/MQL5 Compiler build 1162 from 02 Jul 2015):

mql.exe [<flags>] filename.mq5
        /mql5     - compile mql5 source
        /mql4     - compile mql4 source
        /s        - syntax check only
        /i:<path> - set working directory
        /o        - use code optimizer

However the standalone compiler was intentionally removed, so now links point to the installer in favor of MetaEditor.


Much older version of MetaTrader prior to build 600 had metalang.exe included with the platform.

However in build 616, MetaQuotes intentionally has removed the compiler (mql.exe/mql64.exe) from the standard MetaTrader installation.

This means if you upgrade your MT platform (>616), the compiler executable will be removed.