1

I use CMake to generate build files (makefile or project files) across Windows, OSX and Linux, with C and C++ compilers native to those platforms. On Windows I gnerate VS 2010 project, and in Release configuration a following error occures:

Command line error D8016: '/ZI' and '/Ob2' command-line options are incompatible

Clearly, CMake generates optimization and debug information options which are not compatible. This is default CMake configuration, I do not setup any special flags.

I fix this by altering /ZI to /Z7 in the project options in Visual Studio, but this makes it annoying to setup continous integration system - I would need to add script to modify a project file.

Can I force CMake to gnerate /Z7 (C7 Compatible debug info) instead of /ZI?

In addition, how can I make it generate this only in Release configuration and not in Debug?

1 Answer 1

7

I don't think CMake applies /ZI to Release flags by default - it's probably being applied elsewhere in your CMakeLists.txt or one of the CMake files included by it.

Nonetheless, you can switch the flag by doing something like:

if(MSVC)
  string(REGEX REPLACE "/Z[iI7]" ""
         CMAKE_CXX_FLAGS_RELEASE
         "${CMAKE_CXX_FLAGS_RELEASE}")
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Z7")
endif()

The first line strips all /Zi, /ZI and /Z7 flags if they already exist in the Release flags. Note the use of " round "${CMAKE_CXX_FLAGS_RELEASE}". It's important that this CMake variable remains a single string. If the quotes are removed, the variable becomes a semi-colon separated list (i.e. each space is replaced by a semi-colon) and this is unsuitable for passing to the compiler.

You can give conflicting flags here if you want. So if you removed the string(REGEX REPLACE ...) command and simply left the set command, ${CMAKE_CXX_FLAGS_RELEASE} could contain ... /ZI ... /Z7 ...", but as long as /Z7 is the last debug info flag, it "wins" and is the one which finally gets applied.

However, for the tiny amount of effort to clean up the variable first, I feel it's worth stripping conflicting flags.

1
  • +1 for great answer. Your solution actually works, tested it - but the problem was in the CMakeLists.txt file just as you said.
    – zmilojko
    May 20, 2013 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.