Syntax for execute_process with two executables in command line

In my cmake file, there is a line of the form:

  execute_process(COMMAND cmake -E echo "Executing: my_application.exe file_being_analysed.exe --output results_dir"
                  COMMAND my_application.exe file_being_analysed.exe --output results_dir)

In Linux, this works: no problems.

In Windows, I get an error message:

 Error: Only one binary file can be specified.

…which presumably arises from the fact that the two executable files - the one doing the analysis and the one being analysed - occur on the same command line.

The command itself is correct: if I run it exactly as-is from the Windows command line, then it works. This is the required syntax: I can’t change it. So how can I run it from within execute_process?

I’ve tried enclosing everything after the second COMMAND in quotations; I’ve even tried enclosing everything including the second COMMAND in quotations. In the former case, the whole execute_process command seems to be silently ignored (i.e. not even the ‘echo’ takes place), while in the latter case the whole contents of execute_process is echoed but nothing is executed.

What am I missing?

Note that when you specify more than one COMMAND with execute_process(), the two commands are run as one pipeline. The output from the first command is fed as input to the second. This is different to add_custom_command() and add_custom_target() where multiple COMMAND items are executed separately and in order.

Try separating out the commands into two separate execute_process() calls and see if that changes your situation. I also recommend you check for errors for each one (the COMMAND_ERROR_IS_FATAL keyword makes that easy):

execute_process(COMMAND cmake -E echo "Executing: my_application.exe file_being_analysed.exe --output results_dir"
    COMMAND_ERROR_IS_FATAL LAST
)

execute_process(COMMAND my_application.exe file_being_analysed.exe --output results_dir
    COMMAND_ERROR_IS_FATAL LAST
)
2 Likes

Thank you; yes, it works now.

I think I had misunderstood the original problem, but doing what you suggested helped me to flush it out.

In the real code (as opposed to my [over]simplified example), the actual command being executed is represented as a variable, and it turned out that that string did indeed contain another command, after the one I’d showed, but only in Windows - in Linux the variable did not contain that extra command.

So in fact, now I understand that, it appears that in fact:

execute_process(COMMAND cmake -E echo "Executing: my_application.exe file_being_analysed.exe --output results_dir"
                  COMMAND my_application.exe file_being_analysed.exe --output results_dir)

…is absolutely fine, on Windows as well as Linux. This wasn’t CMake’s fault!