Jepessen
Jepessen

Reputation: 12435

Enable hot reload in Visual Studio 2022 with C++ CMake projects

I've a C++ CMake project that I handle with Visual Studio 2022. I've tried to change the code during the debug and to click the "hot reload" button, but I've the following messsage:

Edits were made which cannot be compiled

And in the Visual Studio output window There's the following message:

'Canvas.cpp' in 'mylib.dll' was not linked with Edit and Continue enabled. Ensure that /INCREMENTAL linking is enabled, and the /EDITANDCONTINUE directive is not ignored.

What's the correct way to do it in a CMakeLists.txt, considering that it will be a cross-platform project?

This is my CMakeLists.txt:

cmake_minimum_required (VERSION 3.18)

project (my-project)

add_definitions (-DMY_PROJECT_EXPORTS)

set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON)
set (CMAKE_AUTOUIC ON)
set (CMAKE_AUTORCC ON)

find_package (Qt5 COMPONENTS Core Gui Widgets REQUIRED)

include_directories (${CMAKE_CURRENT_SOURCE_DIR}/../..)

set (PROJECT_SRC
  # A bunch of files, i.e.
  Private/Canvas.cpp
  Private/View.cpp
  Private/Scene.cpp
)

add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
target_link_libraries(${PROJECT_NAME} PUBLIC
  Qt5::Widgets
  Qt5::Gui
)

Upvotes: 4

Views: 3338

Answers (1)

fabian
fabian

Reputation: 82491

Checking the target system, if the build system is msvc and which toolset version is used should allow you to add all necessary restrictions. Simply add the /INCREMENTAL link option in case the correct conditions are met.

Note: I'm not sure if all those conditions are necessary and which toolset version would be the first one to support the functionality but you could just check for the option for the versions you're required to support.

if (MSVC AND WIN32 AND NOT MSVC_VERSION VERSION_LESS 142)

    # option 1: put the following command before the creation of the first
    #           compiled target you want to apply the option to;
    #           this applies to targets created in this dir and subdirectories
    add_link_option($<$<CONFIG:Debug>:/INCREMENTAL>)
    add_compile_option($<$<CONFIG:Debug>:/ZI>)

    # option 2: apply the option on a per-target basis
    target_link_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:/INCREMENTAL>)
    target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:/ZI>)

endif()

If you want to use option 2 and need to apply this to many targets, perhaps a custom function could be created that can be applied unconditionally to all targets where you want to apply the option.

I don't know about any alterative that would allow you to enable this kind of functionality for all compilers supporting this kind of functionality.

You may want to add an option to disable this functionality via cmake cache settings and add it to the checks done in the if.

You may still need to enable the functionality in the Visual Studio options, as described here: https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-enable-and-disable-edit-and-continue?view=vs-2022

Upvotes: 4

Related Questions