IarBuild.exe and preprocessor defines

Technical Note 191030

Architectures:

All

Component:

general

Updated:

12/2/2019 2:34 PM

Introduction

To build projects from the command line, you can use the IAR Command Line Build Utility, IarBuild.exe. A common question is how to pass preprocessor defines on the command line to IarBuild.exe?

Discussion

The compiler arm\bin\iccarm.exe has a -D option, documented in
Help>C/C++ Development Guide.

If you want to compile a file, and define MYDEFINE, you would do:

iccarm.exe main.c -D MYDEFINE=1

However, there is no similar -D option to IarBuild.exe, so you cannot specify preprocessor defines from the command line in this way.

Solution alternative 1 – Use project configurations

This alternative might be a good choice if you have a few product variants that need to be handled differently in your project, based on for example a PROD_TYPE preprocessor symbol. You can create different configurations with Project>Edit Configurations.

In this example, you can create new configurations of your project named ProdType1, ProdType2 and ProdType3 where you define PROD_TYPE differently in Project>Options>C/C++ Compiler>Preprocessor>Defined symbols.

After this, you can call IarBuild.exe with the following arguments, and the PROD_TYPE preprocessor symbol will be set accordingly:

IarBuild.exe project.ewp -build ProdType1
IarBuild.exe project.ewp -build ProdType2
IarBuild.exe project.ewp -build ProdType3

Solution alternative 2 – Use custom argument variables

Instead of using configurations, this alternative might be a good choice if you have, for example, a version number variable that needs to be updated often. This solution uses custom argument variables and provides a -varfile option to IarBuild.exe.

Example

Step 1:
Configure a custom argument variable ARGVAR_VER with Tools>Configure Custom Argument Variables.

Step 2:
Under Project>Options>C/C++ Compiler>Preprocessor>Defined symbols, define a symbol VERSION that uses the custom argument variable ARGVAR_VER, like this:

VERSION=$ARGVAR_VER$

Step 3:
Call IarBuild.exe with the option -varfile pointing out the .custom_argvars file generated by the IDE:

IarBuild.exe test.ewp -make Debug -log all -varfile test.custom_argvars

The .custom_argvars file is an XML file that was generated in the workspace directory. You can edit this file with an external editor or using a script, making it possible to change the value of the VERSION preprocessor symbol automatically.

Conclusion

If you follow the instructions above, it is possible to pass preprocessor defines on the command line to IarBuild.exe.

 

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

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.