Generating code in MQL5 takes so long time. can we do sth about it to make it faster?

 
Hi developers.
When I am trying to compile my code in MQL5 it takes so long time. about 120 sec. the same code in MT4 complies in just a blink. 
Can I do any thing to make the "Generating Code" process faster??
 
I am struggling with a similar issue, although my source base is quite large. APX 4 MB of source. The only solution I found was to use precompiled shared libraries to speed up the compilation process.

This works quite well, I reduced my compile time from about a minute down to below a second.

But it was a huge undertake to make the code be universally compilable as header include, as library and as shared binary include.

But the effort is worth the work. Having not to wait so long just for a small fix or a test to see if something works as expected, is a blessing and gives me much more time to actually code and work instead of waiting for compilation to finish.


 
Dominik Christian Egert #:
I am struggling with a similar issue, although my source base is quite large. APX 4 MB of source. The only solution I found was to use precompiled shared libraries to speed up the compilation process.

This works quite well, I reduced my compile time from about a minute down to below a second.

But it was a huge undertake to make the code be universally compilable as header include, as library and as shared binary include.

But the effort is worth the work. Having not to wait so long just for a small fix or a test to see if something works as expected, is a blessing and gives me much more time to actually code and work instead of waiting for compilation to finish.


Thanks for the replay, Is there any guidance how to use this precompiled library? 

 
It's a bit tricky, but what you will need to do is segment your code to have "collections of functions" and refactor them to be in separate files. All functions you want to have in your library need the keyword "export".

Then you create a new project with the option "library", you include the above files and compile it. Note, you need to include all files separately. Meaning you cannot have descends of includes inside of an included file, they will be ignored. Only one level of include statement is respected.

Then complied and have your shared library.

Now in your main program, where you want to use the exported functions, you need to declare an #import statement, listing all your functions signatures as you want to use them.

The import statement will then include your precompiled library.

If you are using namespaces, be aware, "import" declares a namespace by the name of the library you are importing. And you may not use this namespace before the import statement. Only after the import statement you can add to the namespace additional functions or variables.

Btw, shared libraries do not support classes or structures as export object, only functions can be exported.

 
Dominik Christian Egert #:
It's a bit tricky, but what you will need to do is segment your code to have "collections of functions" and refactor them to be in separate files. All functions you want to have in your library need the keyword "export".

Then you create a new project with the option "library", you include the above files and compile it. Note, you need to include all files separately. Meaning you cannot have descends of includes inside of an included file, they will be ignored. Only one level of include statement is respected.

Then complied and have your shared library.

Now in your main program, where you want to use the exported functions, you need to declare an #import statement, listing all your functions signatures as you want to use them.

The import statement will then include your precompiled library.

If you are using namespaces, be aware, "import" declares a namespace by the name of the library you are importing. And you may not use this namespace before the import statement. Only after the import statement you can add to the namespace additional functions or variables.

Btw, shared libraries do not support classes or structures as export object, only functions can be exported.

Oh, long story, thank you very much, I am gonna try it. Hope it works
(though I am wondering why MQL keeps making things more difficult in its new version.)

 
Dominik Christian Egert #:
It's a bit tricky, but what you will need to do is segment your code to have "collections of functions" and refactor them to be in separate files. All functions you want to have in your library need the keyword "export".

Then you create a new project with the option "library", you include the above files and compile it. Note, you need to include all files separately. Meaning you cannot have descends of includes inside of an included file, they will be ignored. Only one level of include statement is respected.

Then complied and have your shared library.

Now in your main program, where you want to use the exported functions, you need to declare an #import statement, listing all your functions signatures as you want to use them.

The import statement will then include your precompiled library.

If you are using namespaces, be aware, "import" declares a namespace by the name of the library you are importing. And you may not use this namespace before the import statement. Only after the import statement you can add to the namespace additional functions or variables.

Btw, shared libraries do not support classes or structures as export object, only functions can be exported.

I tried this solution, It is smart but it won't work for me because I have many inter related mqh files, I cannot separate all of them. 

 
Well, then you could put them all into one library.

If that's not possible, then you might need to reconsider your code design. Code should always be modular to a certain extent. Like small building blocks.

If that's not the case, then it is most probable you are coding to specific for a certain problem or towards a solution.

This is of course all speaking in a broader aspect of coding, but still also applies to general guidelines for reusability of code.


 
Fatemeh Ameri: Hi developers. When I am trying to compile my code in MQL5 it takes so long time. about 120 sec. the same code in MT4 complies in just a blink. Can I do any thing to make the "Generating Code" process faster??
I don't have that problem but it really depends on how big your project is. How many lines of code are you referring to?
 

Also, assuming you are using conditional compilation between MQL4 and MQL5, there could also be a big difference between the amount of code produced for MQL4 and that for MQL5, or that some part is convoluted or have some kind of compilation problems causing the issue, yet not being reported. An example would be missing braces or brackets but that are balanced, making the flow of the code strange but not report compilation errors. This is just a conjecture on my part.

EDIT: You could use a 3rd party preprocessor to create separate MQL4 and MQL5 source files, only for analysis, so you can compare them and see what the MetaEditor is actually processing and compiling.

 
Fernando Carreiro #:
I don't have the problem but it really depends on how big your project is. How many lines of code are you referring to?

The main file is 1670 lines but it includes 16 mqh file, each of them 2500-3500 lines.
It is a big project. It is a dashboard with so many options.

 
Dominik Christian Egert #:
Well, then you could put them all into one library.

If that's not possible, then you might need to reconsider your code design. Code should always be modular to a certain extent. Like small building blocks.

If that's not the case, then it is most probable you are coding to specific for a certain problem or towards a solution.

This is of course all speaking in a broader aspect of coding, but still also applies to general guidelines for reusability of code.


If I put them together in one library then first of all, edition and debugging it would be a hard task. so many lines in one file. secondly that file compilation time will be taken long time again. then if I edit sth in that file again I have to compile it and it is gonna take about the same time.

I break the project into different modules of course, with 16 mqh file, each of them is responsible for one specific tasks, but they are interconnected. for example module 10 needs some function  from module 8 and 2. module 8 uses some function from module 2 and 3 and so on, they are inter connected not entirely independent. I am not sure. Maybe I have to create them in a total independent blocks.