[CMake] compiler defines for sub projects

Keith Gardner kgardner at zebraimaging.com
Sat Apr 6 08:51:36 EDT 2013


You can make a variable contain the defines you want and the use that variable in the set_target_properties command.  The value of the variable will be inherited by all of the sup directories.

option(FOO_OPTION "enable foo" OFF)
option(BAR_OPTION "enable bar" OFF)

if(FOO_OPTION)
	set(CUSTOM_DEFINE "FOO_X")
elseif(BAR_OPTION)
	set(CUSTOM_DEFINE "FOO_Y")
else
	set(CUSTOM_DEFINE "OTHER")
endif()

set_target_properties(my_lib PROPERTIES COMPILE_DEFINITIONS ${CUSTOM_DEFINE} )

On Apr 6, 2013, at 1:21 AM, Olaf Peter <ope-devel at gmx.de> wrote:

> 
> thanks for your answer.
> 
> I've forgot to mention in the hurry yesterday, that inisde the lib 
> project generator exist which must not compiled with these defines.
> 
> if(FOO_OPTION OR BAR_OPTION)
> 
>   add_executable(generator_target ...)
>   set_target_properties(generator_target ... COMPILE_DEFINITIONS ...)
> endif()
> 
> This is the reason for set_target_properties(my_lib ...)
> 
> So this solution with add_definitions won't work. Possible it could work 
> if I removed those defs from target.
> 
> Thanks,
> Olaf
> 
>> If you want to have all of the sub directories to have the defines 
> present, you could use ADD_DEFINITIONS().
>> 
>> option(FOO_OPTION "enable foo" OFF)
>> option(BAR_OPTION "enable bar" OFF)
>> 
>> if(FOO_OPTION)
>>      add_definitions("-DFOO_X" )
>> elseif(BAR_OPTION)
>>      add_definitions("-DFOO_Y")
>> else()
>>      add_definitions("-DOTHER")
>> endif()
>> 
>> -----Original Message-----
>> From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On 
> Behalf Of Olaf Peter
>> Sent: Friday, April 05, 2013 12:58 PM
>> To: cmake at cmake.org
>> Subject: [CMake] compiler defines for sub projects
>> 
>> Hello,
>> 
>> is it possible to inherit compiler defines for sub projects in sub 
> directories?
>> 
>> I have a library project which can be configured at compile time 
> using preprocessor defines (cmake option)
>> 
>> option(FOO_OPTION "enable foo" OFF)
>> option(BAR_OPTION "enable bar" OFF)
>> 
>> 
>> if(FOO_OPTION)
>>      set_target_properties(my_lib PROPERTIES COMPILE_DEFINITIONS 
> "FOO_X" )
>> elseif(BAR_OPTION)
>>      set_target_properties(my_lib PROPERTIES COMPILE_DEFINITIONS 
> "FOO_Y")
>> else()
>>      set_target_properties(my_lib PROPERTIES COMPILE_DEFINITIONS 
> "OTHER" )
>> endif()
>> 
>> now, in the sub of this I have unit tests which need to know about 
> this compile defs, hence I have the same if/else again.
>> 
>> Is there an easy way for this?
>> 
>> Thanks,
>> Olaf
>> --
>> 
>> Powered by www.kitware.com
>> 
>> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
>> 
>> Please keep messages on-topic and check the CMake FAQ at: 
> http://www.cmake.org/Wiki/CMake_FAQ
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>> 
> 
> --
> 
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list