6.5.1.7 Configuration Macros

When you add a new or duplicate configuration, you can create your own configuration-related macros or use autogenerated macros for #ifdef or other statements in code. These are MPLAB XC C compiler preprocessor macros.

User-Defined Macros

To define your own preprocessor macro:

  1. In the Project Properties window, click on a project configuration to make it active. Under that configuration, click on the compiler in the toolchain.
  2. Under the Preprocessing and messages options category, locate the option that allows you to define a macro and click on the associated text box.
  3. In the pop-up dialog, enter a macro name and click OK. This macro is now associated with the active configuration.

    You can name the macro the same as a configuration (such as My_Debug from 6.5.1.3 Add a Duplicate Configuration) or you can give it any other name (such as DebugConfig).

    Macro names (and strings, discussed below) are case-sensitive.

You can now use the preprocessor macro in conditional text:

// If My_Debug configuration is active
#ifdef DebugConfig
fprintf(stderr,"This is a debugging message\n");
#endif

In addition, you can assign a string value that matches the configuration name to your macro(s) for use in regular code:

#ifdef DebugConfig
#define CFG_NAME “My_Debug”;
// other defines
#endif
#ifdef DefaultConfig
#define CFG_NAME “My_Default”;
// other defines
#endif
:
int main(int argc, char** argv)
{
printf(CFG_NAME);
return{EXIT_SUCCESS};
}

The CFG_NAME macro will always hold the active configuration's name.

Autogenerated Macros

When a new or duplicate configuration is created, for example My_Test, a related macro is also created, for example XPRJ_My_Test. The macro will have a string value which matches the name of the configuration (My_Test).

You can use the macro for #ifdef statements as in the previous section, or you can use the value of the macros at compile time as follows:

int main(int argc, char** argv)
{
printf(CFG_NAME);
return{EXIT_SUCCESS};
}

The CFG_NAME macro will automatically hold the active configuration's name.