[CMake] Using two sets of compiler flags in same build

André Caron andre.l.caron at gmail.com
Wed Apr 18 06:43:31 EDT 2012


Hi Petr,

I've indeed done some more reading and figured out I could move the
different targets in two sibling sub-directories.  In retrospect, this
seems kind of obvious, but the whole variable inheritance thing is not
the most prominent part of the documentation...

Anyways, I've implemented it and it works like a charm.  I've also
updated the StackOverflow answer to include this information.

Thanks,
André

On Wed, Apr 18, 2012 at 4:40 AM, Petr Kmoch <petr.kmoch at gmail.com> wrote:
> Hi Andre,
>
> I can think of two ways. One is using target properties COMPILE_FLAGS
> and COMPILE_DEFINITIONS to setup flags on a per-target basis.
> Unfortunately, there's no per-config version of those :-(
>
> Another is to separate your targets into directories added by
> add_subdirectory(). I believe you can then override variables in the
> subdirectory without affecting the parent directory. Note that these
> are parent/subdirectory in CMake sense, they can actually be siblings
> or unrelated on disk.
>
> Petr
>
> On Tue, Apr 17, 2012 at 10:01 PM, André Caron <andre.l.caron at gmail.com> wrote:
>> Hi all,
>>
>> I've posted this issue on StackOverflow[1] and have not yet received a
>> suitable response, so I thought I'd ask here instead.
>>
>> Basically, I need to have a single build that uses two sets of
>> compiler flags.  One set is used to build native windows applications,
>> and the other to build a managed (C++.net) application.  It is my
>> understanding that CMake kind of goes out of its way so that compiler
>> flags are uniform throughout the project because this is usually the
>> Right Thing(TM) -- actually, this is one of the reasons I like CMake.
>>
>> How can I create a CMake build script so that I have a subset of
>> targets that build using a specific set of compiler flags, and another
>> subset of targets that build with another set of compiler flags?
>> Please keep in mind that I can't "switch" between either set of flags
>> like I would for debug and release modes because I'm always building
>> all the targets.
>>
>> I currently have this little helper macro (I understand why this is
>> wrong, see below):
>>
>> # Compile managed (.NET) program.
>> macro(add_managed_executable target)
>>   # Enable managed C++ extensions.
>>   if(NOT ${CMAKE_CXX_FLAGS} MATCHES "/clr")
>>     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clr")
>>   endif()
>>   # Remove incompatible settings.
>>   if(${CMAKE_CXX_FLAGS_DEBUG} MATCHES "/RTC")
>>     string(REGEX REPLACE
>>       "/RTC(.+)" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}"
>>     )
>>   endif()
>>   if(${CMAKE_CXX_FLAGS_DEBUG} MATCHES "/MT")
>>     string(REGEX REPLACE
>>       "/MT(.+)" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}"
>>     )
>>   endif()
>>   if(${CMAKE_CXX_FLAGS_DEBUG} MATCHES "/MD")
>>     string(REGEX REPLACE
>>       "/MD(.+)" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}"
>>     )
>>   endif()
>>   # Add target.
>>   add_executable(${target} WIN32 ${ARGN})
>> endmacro()
>>
>> However, as expected, this completely screws up my build because
>> changing CMAKE_CXX_FLAGS* affects the other targets, not only the one
>> specified in the add_executable() call inside the macro.
>>
>> Thanks,
>> André
>>
>> [1]: http://stackoverflow.com/q/10167175/313063
>> --
>>
>> 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