0
votes

I'm coding in C using CMake and I would like to define sobre preprocessor macros in a separate file. The purpose of this is to have a main configuration file for my apps.

I was tinking in doing something similar to the kernel .config files, but I didn't find how to pass a file with definitions to the compiler. I know the option -D of gcc to define a single macro, but it does not accept files as imput. I also know the ADD_DEFINITIONS utility of CMake but the usage but it is also expected to be used with a certain amount of macros, not a file.

Summarizing, I would like to define a file .config with macro definitions:

CONFIG_MACRO_A=y
CONFIG_MACRO_B=y
# CONFIG_MACRO_C is not set
CONFIG_MACRO_C=y

And employ each line as a preprocessor macro in compilation time.

Thank you so much for the help.

1

1 Answers

0
votes

I see at least three possibilities:

  1. Write your config file in the style of a normal header file and include it, i.e.

    #define CONFIG_MACRO_A y
    #define CONFIG_MACRO_B y
    //#define CONFIG_MACRO_C # C is not set
    #define CONFIG_MACRO_D y
    
  2. Use the config file mechanism as @usr1234567 suggested.

  3. Use shell processing in your compiler call (i.e., in the makefile), e.g. for bash:

    gcc $(CFLAGS) `while read mac;  do echo -n "-D$mac "; done < <( grep -v "#" mymacro ) ` $(TARGET)