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

André Caron andre.l.caron at gmail.com
Tue Apr 17 16:01:09 EDT 2012


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


More information about the CMake mailing list