Skip to main content
Associate
January 23, 2024
Solved

Change preprocessor values with headless build?

I'm trying to change a preprocessor value when building a project through the command line headless build.

I can add a value through the command line using -D MY_VAL=10 which works fine if that value has not previously been defined.

 

But I would like to have a default value defined in the IDE and then optionally overwrite it when using the command line. Setting a value in "Project Properties > C/C++ Build > settings > GCC Compiler > preprocessor" means that value will be used and the value from the command line headless build will be ignored.

Is there a way to have a default value and overwrite it through the command line? Thanks,

    This topic has been closed for replies.
    Andrew Neil
    Andrew NeilBest answer
    Super User
    January 23, 2024

    How about:

    #if !defined YOUR_SYMBOL
    #define YOUR_SYMBOL <your value>
    #endif

     So, if it's defined on the command line, the command-line definition will be used; otherwise the definition in the code will be used.

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.

    3 replies

    Andrew Neil
    Andrew NeilBest answer
    Super User
    January 23, 2024

    How about:

    #if !defined YOUR_SYMBOL
    #define YOUR_SYMBOL <your value>
    #endif

     So, if it's defined on the command line, the command-line definition will be used; otherwise the definition in the code will be used.

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tom_yAuthor
    Associate
    January 23, 2024

    That's it, Thanks.

    Andrew Neil
    Super User
    January 23, 2024

    The same approach also works with symbols which may or may not be defined in the IDE project.

    If there's a symbol which needs to be defined on the command line (or in the IDE Project), but doesn't make sense to have a hard-coded default in the code, you could do:

    #if !defined YOUR_SYMBOL
    #error "YOUR_SYMBOL must be defined!"
    #endif

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.