[Cmake] Setting CXX flags

Brad King brad . king at kitware . com
Mon, 16 Jun 2003 16:54:31 -0400 (EDT)


> as I found out, pre-setting CXX_FLAGS for the cache file in the highest
> CMakeLists.txt file is necessary - before the PROJECT(...) keyword. But
> how to tell CMake to use different CXX_FLAGS in different directories
> (of course - in one project)?

You can create a separate cache entry for each directory's flags:

----- Foo/CMakeLists.txt -----

# Let the user choose the flags used for this directory.
SET(FOO_CXX_FLAGS "" CACHE STRING "Flags to use in directory Foo")

# Add the user's chosen flags to the build for this directory.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FOO_CXX_FLAGS}")

----- Bar/CMakeLists.txt -----

# Let the user choose the flags used for this directory.
SET(BAR_CXX_FLAGS "" CACHE STRING "Flags to use in directory Bar")

# Add the user's chosen flags to the build for this directory.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${BAR_CXX_FLAGS}")

Then you can put global flags in CMAKE_CXX_FLAGS in the cache and
directory-specific flags in FOO_CXX_FLAGS and BAR_CXX_FLAGS.  You may, of
course, choose any name for these other entries.

-Brad