Search code examples
clang++preprocessor-directive

Pass preprocessor definition to clang++


Given

void foo() {
    int i = 0;
    #ifdef MACRO_A
         // custom behaviour
    #endif
    // program code
}

Is it possible to pass #define MACRO_A to clang++ when compiling to allow the 'custom behavour' statements to come into effect? I cannot find documentation which suggests this is possible in clang++, but it IS possible in other compilers (g++).


Solution

  • The command, thanks to pevasquez's help, is

    clang++ -D MACRO_A main.cpp -o main
    

    The function

    void foo() {
        int i = 0;
        #ifdef MACRO_A
             // custom behaviour
        #endif
        // program code
    }
    

    will compile with the code in the pre-processor directive section being included.

    This will be an addition to my makefiles to create a debug friendly make-chain as well and the production version.