Build Actions (pre-build and post-build)

Technical Note 99436






2019-12-05 10:37


This Technical Note discusses the use of pre- and post-build actions.


Pre-build and post-build command line environment

When specifying pre-build and post-build action command lines:

  • IAR Embedded Workbench argument variables can be used
  • The directory $TOOLKIT_DIR$\bin is included in the environment variable PATH
  • The initial working directory is $EW_DIR$\common\bin

Using IAR Embedded Workbench for Arm as an example, any of the following commands can be used to display the version of ielfdumparm.exe in the build log window:

$TOOLKIT_DIR$\bin\ielfdumparm.exe --version
ielfdumparm.exe --version
ielfdumparm --version

Figure 1 ielfdumparm --version

Shell commands

To use shell-internal commands, such as DIR, ECHO, PATH, or REM, a shell must be started. See the screenshot below for an example.

Figure 2 - CMD.EXE /C DIR "$PROJ_DIR$\*.c"

Shell I/O redirection

Using the shell, it is possible to redirect output from shell commands and executables to a file. The following examples use a fictitious Windows executable named app.exe.

Example I/O 1: Interactively at a Windows command prompt:

app.exe > output.txt 2>&1

Example I/O 2: From a build action command line:

CMD /C "app.exe > $PROJ_DIR$\output.txt 2>&1"

Example I/O 3: The same applies when only one "CMD /C" leaves the output file empty:

CMD /C "CMD /C "app.exe > $PROJ_DIR$\output.txt 2>&1""

Using .cmd files

Using a .cmd file, you get a detachable command shell automatically. Therefore, you need one less "CMD /C" compared with a plain build action command line.

For example, with a file build.cmd located in the project directory, an alternative to Example I/O 3 could be a build action like

  $PROJ_DIR$\build.cmd "$PROJ_DIR$"

And a file build.cmd containing

  PATH > %1\output.txt 2>&1

How to create a detachable process, using a Visual basic script (.vbs)

In IAR Embedded Workbench for Arm 8.20.2 (and later), a detachable command shell (.cmd) cannot be used, however, a detachable process can be used, and from that process the .cmd can be invoked. The only drawback is that the I/O input back to IAR Embedded Workbench cannot be used.

A detachable process can be created from a Visual basic script (file extension .vbs).

  1. Make a .vbs script like redir.vbs and write a .cmd file, for example like the_doer.cmd.
  2. Put the line Wscript $PROJ_DIR$\redir.vbs "cmd /c $PROJ_DIR$\the_doer.cmd" in your post-build step.

The redir.vbs file looks like:

Dim runner
Set runner = CreateObject("WScript.Shell")
args = WScript.Arguments(0)
Call runner.Run(args, 1, true)

The the_doer.cmd file looks like:

Echo "I am doing something"


Figure 3 – ' Wscript $PROJ_DIR$\redir.vbs "cmd /c $PROJ_DIR$\the_doer.cmd" '

Example files

Some example files are available via this link. Note that these project files (.ewp) require IAR Embedded Workbench for Arm 7.80 or later.

The example files for the detachable process are available via this link. Note that is in IAR Embedded Workbench for Arm 8.20.2 or later that detachable process can be used.

Some further advices

  • To see output from build actions in the Build Log window, use the context menu in that window and select All.
    • In an older IDE (that is, common components 7 or older), enable the option Tools>Options>IDE Options>Messages>Show build messages>All.
    • When the project is up-to-date, the build actions will not be invoked, that is, build actions are only performed if at least one tool in the toolchain is invoked.
      • See example configuration XA for one way to ensure that build actions are invoked.
      • IAR Embedded Workbench argument variables and Microsoft environment variables are similar. However, they are not the same. To use the value of an argument variable like $PROJ_DIR$ in a .cmd file, pass the wanted argument variable as a parameter.
      • If the pre-build or post-build command returns an error code (return value != 0), the entire Build/Make command is aborted.
        • See example configuration XB.


This Technical Note discusses the use of pre-build and post-build actions as available in IAR Embedded Workbench.

While based on practical use of Microsoft products and documentation, these comments and suggestions are to be viewed as guidelines and inspiration.

All product names are trademarks or registered trademarks of their respective owners.


Det här innehållet finns tyvärr inte på svenska.

Vår webbplats finns främst på vårt koncernspråk engelska, förutom det innehåll för investerare som vi är lagstadgade att kommunicera på svenska. Vi rekommenderar att du besöker vår globala webbplats på engelska för att få en bättre upplevelse.

Vi stöder inte längre Internet Explorer. För att få bästa möjliga upplevelse av rekommenderar vi att du uppgraderar till en modern webbläsare som Chrome eller Edge.