[CMake] Source List Compilation Depending on Configuration

Nils Gladitz nilsgladitz at gmail.com
Mon Jul 21 06:12:41 EDT 2014


On 07/21/2014 11:46 AM, Jörg Kreuzberger wrote:
> Hi!
>
> if i want to add sources to list of compilation i can rely on CMAKE_BUILD_TYPE.
> E.g if i want to add a source file only for release, i append it to list of source files only then.
>
> BUT: how can i do this for multi configuration like vs?
> So i want to add it to the sources, so that it appears in vs, but only want to compile it in release build?
>
> Is there an easy way to handle this?

I don't think CMake supports configuration specific source files (I 
assume this might not be supported by all target build systems?).

It should be possible to use the preprocessor (with configuration 
specific defines through the COMPILE_DEFINITIONS_<CONFIG> property) to 
work around it.

E.g. either guard the source file's content itself with
   #ifdef CONFIG_SPECIFIC_DEFINE
   <source file content>
   #endif

or add a wrapper source file that does something like

   #ifdef CONFIG_SPECIFIC_DEFINE
   #include "config1.cpp"
   #endif

or even

   #ifdef CONFIG_SPECIFIC_DEFINE
   #include "config1.cpp"
   #else
   #include "config2.cpp"
   #endif

Nils



More information about the CMake mailing list