IarBuild.exe and preprocessor defines

Technical Note 191030

Targets:
All

Component:
General

Updated:
12/2/2019 1:28 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.

Configuration example 1

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

Configuration example 2

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.

Custom argvars example 1

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$

Custom argvars example 2

 

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

Custom argvars example 3

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.

© IAR Systems 1995-2020 - All rights reserved.

We use cookies on this website to provide you with a better experience. You need to accept cookies to continue using this site. Cookies