Visual Studio with CMake and static library /Z7 flag

Viewed 1443

I'm trying to build a static library (specifically, Protobuf) using Visual Studio 2017 and a CMake based project (and the default Ninja backend).

I want to pass the /Z7 compiler switch when building (or more precisely, replace the default /Zi with /Z7), so that the debugging information ends up embedded in the .lib file rather than creating separate .pdb files.

I know absolutely nothing about CMake (but I do otherwise have a working build). How do I do this?

Preferably, if possible, using only changes to the VS-created CMakeSettings.json file, without making any changes to the upstream CMakeLists.txt files.

If that's not possible, then some way to make a new CMakeLists.txt file which inherits everything from the unmodified upstream file but overrides this one setting would also be good.

My current CMakeSettings.json file specifies both Debug and RelWithDebInfo configurations.

2 Answers

After experimenting for a bit (and inspired by this question), this appears to work as the second (less preferred) solution of adding a new CMakeLists.txt file:

cmake_minimum_required(VERSION 3.1.3)
project(protobuf C CXX)

string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")

add_subdirectory(../cmake cmake)

I'm still interested in cleaner solutions, if anyone has any.

I was searching for this as well. What I wound up doing was (in the CMakeLists.txt file):

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Z7")

...and that seemed to only apply the /Z7 flag when I built in debug (the release binaries were the same size as before). Like you (at least at the time you wrote this), I'm not an expert in CMAKE. I hobbled this together from looking at files online.

Related