Conditionally Compiled Code in Project Configurations

Last modified by Microchip on 2023/11/10 11:05

Multiple project configurations make it possible to use the same code base with different sets of project properties. For example, many projects are targeted at multiple platforms where a single code base may be compiled for different target microcontrollers. This is often accomplished with a series of #ifdef directives to select specific blocks of code only when a particular microcontroller is chosen as the target. The MPLAB® X IDE project configurations can help make working with multiple target platforms very easy.

For this example, assume we have two versions of the target hardware that are very similar and use the same code base. One version has more features and might even use a PIC® microcontroller with more memory and a couple of extra peripherals. Let's call them the Cheap Version and the Expensive Version.

First, create a project with multiple configurations. Name one configuration "Cheap_Version" and the other "Expensive_Version" (or whatever describes them best for your situation).
Project Properties window showing multiple configurations
Click image to enlarge.

Select the C compiler Node for each configuration, and select the C compiler node in the tree along the left side (the compiler itself, not the suite heading). Under the General category (combo box at the top center of the window) click on the ellipsis () to the right of Preprocessor Macros and create a macro label to identify the configuration. For example, in the "Cheap_Version" configuration create a macro label "CHEAP_VERSION" and in the "Expensive_Version" configuration create a macro label "EXPENSIVE_VERSION".
Other options may be changed between the two configurations such as which PIC microcontroller will be the target, compiler settings, and so on. The two configurations can be completely different except that they share the same source files.

Preprocessor Macros in General Options settings

If you are using one of the MPLAB® XC compilers, the preprocessor macros are now part of the Global Options (top node of the compiler toolchain in the tree) and are defined under the Global Options category on the Define common macros line.

Defining common macros for configuration


In your code, you can now use the macro labels you just defined to identify which blocks of code should be used for each configuration. For example:

#ifdef CHEAP_VERSION
   //Code specific to cheap version here
#endif
#ifdef EXPENSIVE_VERSION
   //Code specific to expensive version here
#endif

The combo box on the toolbar may be used to switch between the two configurations. When you build the project, it will use the options for the selected configuration and will correctly select conditional code blocks inside #ifdef directives based on the macro label provided. 

Although the editor generally colors unused #ifdef blocks the same as comments, you might need to save the project after changing configurations for the colors to change properly.

Combo box showing configuration choices
Click image to enlarge.