[CMake] How to build 2 targets from the same source, differing in -D_SWITCHES_ only

Hendrik Sattler post at hendrik-sattler.de
Sat Sep 5 03:54:10 EDT 2009


Am Freitag 04 September 2009 21:51:59 schrieb Alexander Neundorf:
> On Thursday 27 August 2009, Christian Ehrlicher wrote:
> > > Von: "Marcel Loose" <loose at astron.nl>
> > > CC: cmake at cmake.org
> > > Betreff: Re: [CMake] How to build 2 targets from the same
> > > source,	differing in -D_SWITCHES_ only
> > >
> > > Hi Eike,
> > >
> > > I think the only safe and reliable way to do this is create several
> > > build directories, e.g. build/type_1 and build/type_2.
> > > When running cmake in build/type_1, you should add -D_TYPE1_ to the
> > > preprocessor flags; same for build/type_2.
> >
> > Why? Does SET_TARGET_PROPERTIES() with COMPILE_FLAGS not work?
> 
> So the full code is:
> 
> add_executable(foo main.c)
> set_target_properties(foo PROPERTIES COMPILE_FLAGS -D_TYPE1_)
> 
> add_executable(bar main.c)
> set_target_properties(bar PROPERTIES COMPILE_FLAGS -D_TYPE2_)

Actually better:
set (foo_DEFS _TYPE1_)
set (bar_DEFS _TYPE2_)
foreach(i foo bar)
  add_executable(${i} main.c) 
  set_target_properties(foo PROPERTIES COMPILE_DEFINITIONS ${${i}_DEFS})
  foreach(flag RELEASE DEBUG)
    set_target_properties(foo PROPERTIES COMPILE_DEFINITIONS_{flag} 
${${i}_DEFS})
  endforeach(flag)
endforeach(i)

 ;-)

HS


More information about the CMake mailing list