How to make Visual Studio 2022 Hot Reload Actually Work in CMake Projects

So I’m trying to set up CMake on Windows to use this new cool feature called Edit and Continue (or Hot Reload). By default, surprisingly, it just doesn’t work. Maybe it works for usual Microsoft style C++ projects. I don’t know, but it doesn’t work for builds, even new, even if I go and create a new project!

Hit debug and make a simplest change. I can’t do anything. It just shows the following error message.

Edits were made which cannot be compiled….

But it’s a simplest change I can think of! Must be something wrong with me.

So! It turns out you need to force some CMake flags in order for this to work. Welcome to the Microsoft world and open source! To just enable it for all of the targets you can set the following CMake debug flags:

set(CMAKE_CXX_FLAGS_DEBUG "/MDd /ZI /Ob0 /Od /RTC1")

Of course, if you want to limit it by the compiler, you can always wrap it in if statements:

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
	set(CMAKE_CXX_FLAGS_DEBUG "/MDd /ZI /Ob0 /Od /RTC1")
endif()

My full root CMakeLists.txt looks like this (somewhat simplified):

cmake_minimum_required (VERSION 3.8)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project ("bt"
		VERSION 1.2.0)

if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
	set(CMAKE_CXX_FLAGS_DEBUG "/MDd /ZI /Ob0 /Od /RTC1")
endif()

add_subdirectory("core")
add_subdirectory("bt")

It makes a difference where you put the set statement, it should be before you add targets.

Now, if I run my debugging session, and in this case, I’m using it to build a user interface for one of my small utilities, I can just change the UI code on the fly and it updates! Instantly! Like in a clip below. Cool, isn’t it?


To contact me, send an email anytime or leave a comment below.