6

I would like to disable the optimization of a single *.cpp file within my CMake project. I found out that CMake provides the following to achieve this:

SET_SOURCE_FILES_PROPERTIES(${FILE} PROPERTIES COMPILE_FLAGS -O0)

However, this seems not to work for me. I generate a Visual Studio 2013 Project and whenever I change to 'Release' or 'RelWithDebInfo' it still fully optimizes that file as can be seen under the properties.

Does it matter where the above command is placed? I have multiple cmake files distributed over the whole project. I placed the above command directly in the cmake file where the *.cpp file gets added to the project.

Or is there any other way to tell CMake that this file should not get optimized at all?

Simon
  • 706
  • 6
  • 23
  • 2
    It should work. The command should be placed within the same cmake file, where `add_executable()` / `add_library()` is called. See also documentation for [set_source_files_properties](https://cmake.org/cmake/help/v3.0/command/set_source_files_properties.html) – Tsyvarev Nov 05 '15 at 09:36
  • This will append -O0 to the compile options, but if the compile is release won't you end up with both -O0 and -O3 on the command line? – Cameron Lowell Palmer Dec 21 '17 at 11:59

1 Answers1

2

Thanks Tsyvarev!

Indeed I had to place the command in the cmake file where the according add_library() is contained in order to make it work.

But in addition there was also a small change I had to apply: Visual Studio needs -Od (instead of -O0) to disable optimization.

So the final command for Visual Studio builds looks like this:

SET_SOURCE_FILES_PROPERTIES(${FILE} PROPERTIES COMPILE_FLAGS -Od)

and this placed in the cmake file where the add_library() call is for that *.cpp file.

damian
  • 3,604
  • 1
  • 27
  • 46
Simon
  • 706
  • 6
  • 23