Executing Multiple Commands as Post-Build Steps in Eclipse

The GNU ARM Eclipse plugins from Liviu already offer several built-in actions which can be performed at the end of a build: creating flash image, create listing file and printing the code and data size:

GNU ARM Eclipse Extra Post Build Steps

GNU ARM Eclipse Extra Post Build Steps

But what if I need different things, or even more things?

Post-Build Steps

For this there is the ‘Post-build steps’ settings I can use: that command is executed at the end of the build:

💡 Be aware that by default pre-build scripts might not wait until finished before starting the build. Disabling parallel build avoids the problem — see https://community.nxp.com/message/1058714

Print Size PostBuild Step

Print Size Post-Build Step

❗ The post build step is only executed if sources files have been compiled and linked. If you want to enforce that there is always a ‘true’ post build, then you need to delete some files in the Pre-build step to enforce a compilation and a link phase.

Multiple Post-Build Steps

But what I need more than one action in the post-build step? I could call a batch or script file, but this is probably an overkill in too many cases, and adds a dependency to that script file. A better approach is to directly execute multiple commands as post-build step.

Unfortunately, the documentation found about the post-build step with a web-search is misleading (e.g. in the Eclipse Luna documentation):

“Command: Specifies one or more commands to execute immediately after the execution of the build. Use semicolons to separate multiple commands.”

Unfortunately, semicolons is plain wrong (at least did not work for me) :-(. The solution is to use ‘&‘ (ampersand) to separate multiple commands on Windows:

💡 On Linux, use the ‘;’ to separate commands as noted in the documentation/help, and use ‘&’ on Windows. Unfortunately, this makes project not cross-platform.

Multiple Post-Build Commands

Multiple Post-Build Commands

And this works for me, at least under Windows 7 :-).

Links related to that topic:

Happy Post-Stepping 🙂

33 thoughts on “Executing Multiple Commands as Post-Build Steps in Eclipse

  1. The “&” (ampersand) symbol is the windows command line equivalent of the linux “;” (semicolon) command line to continue to the next command in the list.
    So in Windows we have:
    cmd1 & cmd2 & … cmdN

    In most Linux (BASH / DASH) the equivalent is:
    cmd1; cmd2; … cmdN

    The “pre-build” and “post-build” steps are just echo’d to a CLI.
    You’re absolutely correct Eric, the documentation is too centric on the *inx world for those build options.

    Like

    • Hi Jonathan,
      I had no time to try it on Linux, but I already expected something like this. Thanks for the confirmation, I will add a note to the article about that it indeed works with “;” on Linux. Thanks!

      Like

  2. Pingback: CRC Checksum Generation with ‘SRecord’ Tools for GNU and Eclipse | MCU on Eclipse

  3. Pingback: GNU Static Stack Usage Analysis | MCU on Eclipse

  4. Pingback: Aligning S19 Records to 64-bit Boundaries | MCU on Eclipse

  5. Pingback: Merging S19 Files | MCU on Eclipse

  6. I use winxp. The & doesn’t help when I want to run multiple post-build commands. Only the last command after & runs, the previous commands are reported as wrong argument. Any suggestion on how to fix this problem?

    Like

  7. Pingback: Automatically Refresh Eclipse Projects before Build | MCU on Eclipse

  8. Just to add to the confusion, in *nux systems you can use ‘&&’ to join multiple commands, as in:

    cmd1 && cmd2

    This has the (generally) beneficial effect of only running cmd2 if cmd1 completes without an error.

    Like

  9. By changing the selected builder type in the Project Properties: C/C++ Build/Builder Settings
    to Internal Builder “;” is working for me (I use Win7)
    to External Builder “;” does not work for me

    The reason for this seems to be that the line containing the postbuild steps is copied to the Makefile (using the external Builder) but for any reason “make” does not recognize “;” as command separator.

    So I see two workarounds for this:
    Change “;” to “&” if the external builder is used or use “;” and the internal builder

    Like

  10. How to do a change directory from post-build steps? I tried the same by using ‘cd’ command but the directory is still the same(verified using pwd command).

    Like

    • Not sure what you are trying to do: the current directory of a process spawned by the postbuild process is the project output folder (usually ‘debug’ or ‘release’). But a ‘cd’ with that process won’t change the current directory of Eclipse or any other build tools which are out-of-process.

      Like

  11. <<>>

    I realize this is old, but people are still running into that problem.

    This bug was fixed in Eclipse CDT 9.3, but there is a workaround.

    https://bugs.eclipse.org/bugs/show_bug.cgi?id=340300

    Parallel build using generated makefile doesn’t manage dependencies

    1. Add a makefile.init in the project root with the following content:
    allwithpre :
    $(MAKE) –no-print-directory pre-build
    $(MAKE) –no-print-directory main-build
    2. Remove the pre-build task from the configuration
    3. In the project settings, C/C++ Build -> Behavior -> Workbench Build
    Behavior, Change “all” to “allwithpre”

    Liked by 1 person

  12. Hi Erich, thank you very much as usual for your posts.
    I’m looking for something similar for linked folders. I mean: in my experience if I import files as symbolic links from a folder, that folder (and its content) gets not automatically refreshed in the workspace.
    Do you think it’s possible?

    Best Regards
    Roberto

    Like

  13. As usual this showed up first link on Google search 🙂 , was porting Atollic TrueStudio project from Windows to Linux, great point for multiple commands separated with & in windows and ; in Linux. Solved my issue in an instant. Great for you to share all this stuff.

    Like

  14. Hello Erich
    I have Tried different commands for Executing a batch file in post build Box but nothing occurred ..Can you please provide the Correct Format for a command in Post build box for Executing batch file in the same directory in the project folder ?

    Liked by 1 person

  15. The article is quite old now, yet from my searches these past few days the post-build stuff is still very badly documented.
    Nowhere the details of what you can write to this field are explained properly, so here are some of my tests on STM32CubeIDE 1.10, that uses Eclipse CDT 10.6.2 on Windows 10.

    – Commands are run from the build directory (the place where the .elf is at the end). Asking to run a script that is outside of that folder works, but each command in that script is run from the build directory too, instead of from where the script is.
    – Path line use backslashes for paths (windows-style), unless the command is wrapped with quotes in which case it will accept slashes too (both windows and unix style are allowed then, “either-style”) :
    “../post_build.bat” => OK
    “..\post_build.bat” => OK
    ..\post_build.bat => OK
    ../post_build.bat => fail
    – For multiple commands, both ; and & can be used, but each with different rules.
    Using & switches the path interpreter to unix-style-only, thus path need to use slash (unless they are quoted, which turns them to “either-style” mode). On the other hand, using ; the paths stay in in windows-style mode.
    Neither & or ; are allowed inside quoted string.
    ..\post_build1.bat ; ..\post_build2.bat => OK (; with windows-style)
    “../post_build1.bat ; ../post_build2.bat” => Fail (; inside of quoted string)
    “../post_build1.bat & ../post_build2.bat” => Fail ( OK (; with either-style)
    “../post_build1.bat” & “../post_build2.bat” => OK (& with either-style)
    ../post_build1.bat & ../post_build2.bat => OK (& with unix-style)
    ..\post_build1.bat & ..\post_build2.bat => Fail (& forced unix-style thus windows-style path isn’t valid anymore)

    I hope that can help others.

    Liked by 1 person

  16. Actually, that might not be the complete picture.
    I also have WSL2, and did not think that part of these behavior might be linked to that… it would require tests on a more vanilla Windows install, sorry.

    Liked by 1 person

    • That’s a good point: how the commands get executed depends on the subsystem as well. Eclipse tries to handle things like path/file separators, but things are not standard between Windows, Linux/Mac, unfortunately.

      Like

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.