Home:ALL Converter>How to use a CMake variable to choose which header file to include in a c program

How to use a CMake variable to choose which header file to include in a c program

Ask Time:2020-09-25T21:04:25         Author:Roby

Json Formatter

Is it possible to use a CMake variable to decide which header file will be included in a c program. I have tried the following without success:

header_a.h ->is the file including the following code

#if (@CMAKE_VAR@ == "B")
    #include header.b
#else
    #include header.c
#endif

I have also tried the following with same failed results: header_a.h

#ifdef CMAKE_VAR
#define CMAKE_VAR_SELECTION CMAKE_VAR
#endif

#if (CMAKE_VAR_SELECTION == "B")
   #include header.b
#else
   #include header.c
#endif

Thanks in advance for your help.

Author:Roby,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/64064748/how-to-use-a-cmake-variable-to-choose-which-header-file-to-include-in-a-c-progra
rhaport :

what you can do is to define a corresponding macro for C/C++ code from CMake.\n# your CMAKE variable\nset(CMAKE_VAR "B")\n# that would define macro constant CMAKE_VAR_B \nadd_definitions(-DCMAKE_VAR_${CMAKE_VAR})\n\nThen in the code you can use you proposed\n#if defined(CMAKE_VAR_B)\n #include header.b\n#else\n #include header.c\n#endif\n",
2020-09-25T13:39:17
yy