Elsewhere the question has been asked, "How do I turn off optimizations on one file?" The answer is usually something like this:
cmake_minimum_required( VERSION 3.8 )
project( Hello )
add_executable( hello hello.c foo.c bar.c )
set( CMAKE_C_FLAGS_RELEASE "" )
set( CMAKE_CXX_FLAGS_RELEASE "" )
set_source_files_properties( hello.c
PROPERTIES
COMPILE_FLAGS -O0 )
This works unless you invoke cmake like this:
cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../hello
And you get this in your build.ninja
FLAGS = -O3 -DNDEBUG -O0
Checking the documentation on COMPILE_FLAGS
Additional flags to be added when compiling this source file.
This makes sense, it is added to the list of COMPILE_FLAGS, it does not override existing compiler flags.
So, within CMake how can you override the optimisation level on a single file and being able to compile the rest of the project in Release
? Otherwise you can force the compile to CMAKE_BUILD_TYPE=""
which is the default behavior, but that somewhat defeats a selling point of Cmake.
You can't overwrite compiler options with the
makefile
CMake generators on source file level. Options are always appended (see my answer at Is Cmake set variable recursive? for the complete formula).This is - as far as I know - only supported with the
Visual Studio
solution/project generators. These generators have flag tables to identify flags that are in the same group/that does overwrite a previous defined flag.So yours is more like a feature request to also add compiler option tables to CMake's
makefile
generators.Alternatives
I just wanted to add some crazy CMake magic I came up with as a workaround. Add the following to your main
CMakeLists.txt
after theproject()
command:This example moves the
CMAKE_CXX_FLAGS_<build type>
content into an newCOMPILE_FLAGS
directory property that is then linked toCOMPILE_FLAGS
source file property viadefine_property(... INHERITED ...)
.Now the build type specific flags are only defined in
COMPILE_FLAGS
for each source file and you can overwrite/change them e.g. with the code snippet from your example:References