0

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.

A. Moran
  • 31
  • 3
  • 2
    You can use `config_file`. Check http://stackoverflow.com/questions/38419876/cmake-generate-config-h-like-from-autoconf/38423317#38423317 and http://stackoverflow.com/questions/647892/cmake-how-to-check-header-files-and-library-functions-like-in-autotools – usr1234567 Jul 20 '16 at 06:42

1 Answers1

0

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)
    
Matthias
  • 8,018
  • 2
  • 27
  • 53
  • Thanks! For my use case the third option will do the trick. The main purpose was to make config file 'readable' so I wanted to avoid the use #define or #cmakedefine . – A. Moran Jul 20 '16 at 13:25