Conditionally Compiled Code in Project Configurations

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. MPLAB® X IDE's 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).

Click image to enlarge.


For each configuration, 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 top center of window) click on the "" 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.

Click image to enlarge.

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.

Click image to enlarge.


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:


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.

Click image to enlarge.

20th Annual
Microchip MASTERs Conference 2016
Register now - Deadline: July 29

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.