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:
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
❗ 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.
And this works for me, at least under Windows 7 :-).
Links related to that topic:
- GNU Additional Tools: Create Flash Image, Print Size and Extended Listing Options
- S-Record Manipulation with GNU objcopy and Burner Utility
- S-Record Generation with gcc for ARM/Kinetis
- Converting S19 Files into Binary Files with GNU objcopy
Happy Post-Stepping 🙂
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.
LikeLike
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!
LikeLike
Pingback: CRC Checksum Generation with ‘SRecord’ Tools for GNU and Eclipse | MCU on Eclipse
Pingback: GNU Static Stack Usage Analysis | MCU on Eclipse
Pingback: Aligning S19 Records to 64-bit Boundaries | MCU on Eclipse
Pingback: Merging S19 Files | MCU on Eclipse
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?
LikeLike
Hi Carol,
I don’t have Windows XP any more, so I cannot try this out. I suspect that maybe as on Linux a different separator is needed between the commands?
LikeLike
Pingback: Automatically Refresh Eclipse Projects before Build | MCU on Eclipse
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.
LikeLike
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
LikeLike
By the way, there’s something to watchout for with pre-build scripts: using default Eclipse settings, the build will happily proceed before the pre-build steps complete. Disabling parallel build avoids the problem — see https://community.nxp.com/message/1058714 for full details.
LikeLike
Hi Robert,
good point! I’ll have that added to the text.
Thanks!
Erich
LikeLike
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).
LikeLike
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.
LikeLike
How can I use variable in my c project in post-build command? I need to prepend firmware version in post-builtd. The firmware version also use in my project.
LikeLike
That depends on what kind of variables you want to use: the have to be ‘build’ variables. You can see this from this article where it uses the ${cross_prefix} variable in the post-build command.
I suggest to have a read at https://mcuoneclipse.com/2013/12/22/eclipse-build-variables/ too.
I hope this helps,
Erich
LikeLiked by 1 person
<<>>
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”
LikeLike
Thanks for sharing, very helpful!
LikeLike
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
LikeLike
Hi Roberto,
I have not seen such an issue with linked files and folders, they get properly refreshed. Make sure the project refresh policy is set correctly, maybe it is not pointing to its own project?
I hope this helps,
Erich
LikeLike
Thank you Erich. Interesting what you say…
I always use import from file system wizard and then add files/folder to an existing.
I will check where I am wrong
Thanks again
LikeLike
Hi Roberto,
have a look at the ‘refresh policy’ (https://mcuoneclipse.com/2015/06/06/eclipse-project-refresh-policy-broken-incremental-build-with-external-make/): having that one wrong might cause strange issues.
LikeLike
Hi Erich,
thank you so much the same.
My issue has been solved with linked folders.
I was wrong in linking files instead.
Sorry for having bothered you
Roberto
LikeLike
Hi Roberto,
no worries! Thanks for posting the solution.
Erich
LikeLike
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.
LikeLike
Great, sometimes google can find the right things, and it is even from 2014 :-).
LikeLike
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 ?
LikeLiked by 1 person
It might depend on the IDE used, but for MCUXpresso IDE (11.3.x) it is as simple as:
../test.bat
Keep in mind that it uses sh.exe (not cmd.exe), see https://mcuoneclipse.com/2021/03/28/using-linux-shell-commands-on-windows-with-mcuxpresso-pre-post-build-steps/
LikeLike
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.
LikeLiked by 1 person
Thanks for all the details and tests!
LikeLike
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.
LikeLiked 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.
LikeLike