I would like to change the default values for CMAKE_CXX_FLAGS_RELEASE or CMAKE_CXX_FLAGS_DEBUG in CMake. Basically, I have some default options that are slightly different from CMake (for example, for release), and I don’t need to ask myself: “Oh, does -O3 or our -O2 make them priority when adding using add_compile_options”.
Now I know how to set these values, but I don’t know how to make them editable by users in two usual ways: using - DCMAKE_CXX_FLAGS_DEBUG=yourflags on the command line or setting it using ccmake or CMakeSetup.
The problem is that CMAKE sets and caches its own default values for them, and if you try to overwrite variables without using FORCE, the default values will never be changed. If I use FORCE in my set: set(CMAKE_CXX_FLAGS_DEBUG blah CACHE STRING "" FORCE) , it will overwrite it every time the script is run, which excludes the possibility for the user to change it if he wishes.
I managed to hack it to work with CCMAKE by doing the following, but it still does not work with cmake -DCMAKE_CXX_FLAGS_DEBUG as it overwrites the user change AFTER he did it:
set(DEFAULTS_SET FALSE CACHE BOOL "") set(CMAKE_CXX_FLAGS_DEBUG "-this -that" CACHE STRING "" FORCE) set(DEFAULTS_SET TRUE CACHE BOOL "" FORCE)
Obviously, this is an unpleasant hack and does not fully work (in the case of cmake -Dwhatever = thisorthat). I could add other types of assembly, but I really don't understand why this is necessary to change a few simple things.
Edit March 1, 2015:
I created a solution that works, although I'm still not impressed with what I need to do. I saw other comments that solve the problem of setting CMAKE_CXX_FLAGS_DEBUG and friends without crossing them, but this initially did not work for me because I also tried to select them based on the compiler used. However, the compiler is not defined until it already populates the variables for me. The trick I used is as follows. Before the project team, you must set the flags variables in front of something special.
set(CMAKE_CXX_FLAGS_DEBUG "_UNSET" CACHE STRING "") project(your_project C CXX) if(${CMAKE_CXX_FLAGS_DEBUG} STREQUAL "_UNSET") # Do some compiler switching here and then set your flags with FORCE. set(CMAKE_CXX_FLAGS_DEBUG "-ggdb3 -O0" CACHE STRING "" FORCE) endif()
Now this allows me to select default values that are completely overridden via the command line with -D or in cmake-gui.
c ++ compiler-optimization cmake
Sevalecan Feb 26 '15 at 0:08 2015-02-26 00:08
source share