[Cmake] CMake SET + CMakeCache.txt

Brad King brad.king at kitware.com
Mon Apr 7 09:10:57 EDT 2003


> I am looking for a mechanism for use in a CMakeLists.txt file by which I
> can APPEND to- rather than INITIALIZE- the contents of a cmake variable
> , in this case CMAKE_CXX_FLAGS so that when so that when I run ccmake,
> what I appended and what was already there will appear.
>
> Is that possible ?  I would have expected this is a common thing to want
> to do, since how else can the ccmake user know what has been set if it
> does not appear in the GUI ?

If you really want to do this, you can write

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -foo -bar" CACHE STRING "" FORCE)

However, this will append the values EVERY TIME cmake runs.  You'll then
need some check to see if the flags are already there:

IF(CMAKE_CXX_FLAGS MATCHES "-foo")
  # Flag already there.
ELSE(CMAKE_CXX_FLAGS MATCHES "-foo")
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -foo"
      CACHE STRING "" FORCE)
ENDIF(CMAKE_CXX_FLAGS MATCHES "-foo")

This is strongly discouraged, however.  Adding compiler flags is very
platform-specific.  If it is detected that a certain platform is running,
and it is known that a certain compiler flags is needed (such as
-ftemplate-depth-50 on g++) then you should just append the flag using

  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth-50")

and it will show up in the makefiles.  There is no need to expose this
requirement to the user, however.  Having CMAKE_CXX_FLAGS in the GUI is
mostly there to allow users to select warning/optimization/debug levels.

-Brad




More information about the CMake mailing list