Currently to change a compiler flag when using gcc, I edit the CMakeLists.txt for the build target:
if (UNIX)
add_definitions(-Wall)
add_definitions(-g)
#add_definitions(-O2)
endif (UNIX)
The problem with this is that git picks up the change. If I go ahead and commit this change, I will annoy the other developers who expect to use -O2 instead of -g but they get my version when they pull unrelated changes. Normally, I could just exclude this change from my commits, but when I make an actual change to the CMakeLists.txt file, there is no way to avoid pushing up my personal selection of compile flags.
Is there a way to tell CMake to create a file in the build/ directory (specific to each working copy, and therefore to each developer) that an individual person can modify to their heart’s desire without touching project files (everything but build/). Naturally, our build/ is not committed to the git repository.
It might be helpful to note that when using Visual Studio instead of gcc, the IDE handles this for us through its UI which modifies the VS solution file in build/. The problem is that we have no such mechanism when using GNU Makefiles.
Our project is organized like this:
ourproject/
bin/
build/ <-- CMake-generated stuff goes here
lib/
src/
abuildtarget/
anotherbuildtarget/
source.cpp
source.h
CMakeLists.txt
You are using CMake incorrectly here. The
add_definitionsfunction is not for adding compiler options like you are doing; rather, it is to add pre-processor definitions such asadd_definitions(-DDEBUG).What you want to do is set the
CMAKE_<language>_FLAGSwhen you configure to the options you want. If there is a standard set you need, then put that in the CMakeLists.txt file such as:Where
Fortrancan be replaced byCXXorC.In this case, the
CMAKE_<language>_FLAGS_<build type>sets the flags based on theCMAKE_BUILD_TYPEvariable. If it is set toRelease, then it usesCMAKE_Fortran_FLAGS_RELEASE. We added several other possible build types. If the user wants something that isn’t one of the standard build types, then they setCMAKE_<language>_FLAGSto whatever they want when configuring and it overrides the build type setting and uses the user-defined flags instead.