[CMake] Managed C++/CLI, default build flags and configurations

Michael Hertling mhertling at online.de
Sun Jun 12 13:23:04 EDT 2011


On 06/07/2011 07:29 PM, David Hunter wrote:
> Hi,
> 
> I have a number of projects which contain mostly sub-directories with
> standard C++. I currently build all this stuff on Linux and Windows
> and everything is great. However I have a couple of directories in
> these projects which contain managed C++/CLI code so that I can expose
> the unmanaged C++ functionality to the rest of the Microsoft universe,
> obviously these directories build only on Windows. Currently I include
> the following snippet of CMake code, which I found on another post, to
> "fix" the compiler flags to enable the managed build
> 
> # Managed C++ set up
> 
> set_target_properties(${TargetName} PROPERTIES COMPILE_FLAGS "/clr")
> 
> if(CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1")
>    string(REPLACE "/RTC1" " " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
> endif()
> 
> if(CMAKE_CXX_FLAGS MATCHES "/EHsc")
>    string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
> endif()
> 
> My question is whether there is a better way to do this as the above
> seems a bit brittle and manual. For instance is it possible to define
> a new build configuration and somehow signal in the CMake input file
> that this directory should use it. In general what I want to be able
> to do is to indicate that a different set of default compile flags
> should be used for a given directory rather than the default.
> 
> I am happy to spend time working on this and sharing any results but
> would appreciate any pointers from the CMake experts before I start so
> I don't go off wasting my time.
> 
> David

Due to the various origins of compile flags,

- directory/target/source properties (configuration-specific)
- CMake variables (configuration-specific)
- environment variables like CC or CXX

it's notoriously difficult to manage them thoroughly. IMO, tweaking the
flags as in your above-noted example is the best you can do in your use
case if only a few directories are concerned. Alternatively, you might
provide two different sets of flags, e.g. MANAGED_CXX_FLAGS[_<CONFIG>]
and UNMANAGED_CXX_FLAGS[_<CONFIG>], and enable only one of these sets
in every directory while taking account of the variables' propagation
to subdirectories; of course, the affected flags must be removed from
the general CMAKE_CXX_FLAGS[_<CONFIG>] variables. As a last resort,
with a Makefile generator, you could use the RULE_LAUNCH_COMPILE
directory property to perform really last-minute modifications
of the compiler's command lines in the concerned directories.

Regards,

Michael


More information about the CMake mailing list