Reputation: 631
I'm trying to set up default for compiler flags in the way that can be later changed by user using -DCMAKE_CXX_FLAGS_RELEASE="..." and similar on commandline.
If I use:
SET( CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -DNVALGRIND" )
than flags cannot be changed using commandline or ccmake.
If I use
SET( CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -DNVALGRIND" CACHE STRING "" )
than flags are not set at all.
Is there any right way to do this?
Upvotes: 3
Views: 2434
Reputation: 631
So, by experimenting I figured this out (sort of).
First I found out that the CACHE version does not work because there already is value in cache. That is if I apply FORCE at end of it it gets set:
SET( CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -DNVALGRIND" CACHE STRING "" FORCE )
obviously this would not allow users to specify flags by themselves, making it equivalent to first option.
The solution is:
Put cache setting command just at the beginning of cmake file (before project command), somehow this sets values before cmake sets them internally. So now it looks like this:
SET( CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -DNVALGRIND" CACHE STRING "" )
SET( CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG -DNVALGRIND" CACHE STRING "" )
...
project( whatever )
...
and it works. I guess that this will be bad if you use compilers which require some different default flags. But then you should not set default by yourself anyway.
I'm still wondering if there is cleaner way.
Upvotes: 3