Search code examples
includemakefileconditional-compilation

conditional compliation based on variable into makefile


Inside my C/C++ code I would like to include or not a file depending on different compilation.

For the moment I use this:

#ifndef __x86_64__
    #include <myLib.h>
#endif

this gives me the possibility of doing whether the platform is 32/64 bit but does not give me enough freedom.

I would like to pass a variable to my makefile like

make includeMyLib=1

and depending on this having something like:

#ifndef includeMyLib
    #include <myLib.h>
#endif

Do you know if anything like this is possible?


Solution

  • If you use GNU make, you could have something like this in the Makefile:

    ifdef includeMyLib
    CFLAGS += -DincludeMyLib
    endif
    

    This will change the flags used by the compiler to add the #define includeMyLib.