[英]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:我有一个使用 Visual Studio 2022 处理的 C++ CMake 项目。我尝试在调试期间更改代码并单击“热重载”按钮,但我收到以下消息:
Edits were made which cannot be compiled进行了无法编译的编辑
And in the Visual Studio output window There's the following message:在 Visual Studio output window 中有以下消息:
'Canvas.cpp' in 'mylib.dll' was not linked with Edit and Continue enabled. 'mylib.dll' 中的 'Canvas.cpp' 未与启用的编辑和继续链接。 Ensure that /INCREMENTAL linking is enabled, and the /EDITANDCONTINUE directive is not ignored.确保启用 /INCREMENTAL 链接,并且不忽略 /EDITANDCONTINUE 指令。
What's the correct way to do it in a CMakeLists.txt
, considering that it will be a cross-platform project?考虑到它将是一个跨平台项目,在CMakeLists.txt
中执行此操作的正确方法是什么?
This is my CMakeLists.txt
:这是我的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
)
Checking the target system, if the build system is msvc and which toolset version is used should allow you to add all necessary restrictions.检查目标系统,如果构建系统是 msvc 以及使用的工具集版本应该允许您添加所有必要的限制。 Simply add the /INCREMENTAL
link option in case the correct conditions are met.如果满足正确的条件,只需添加/INCREMENTAL
链接选项。
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(/INCREMENTAL)
# option 2: apply the option on a per-target basis
target_link_options(${PROJECT_NAME} PRIVATE /INCREMENTAL)
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.如果您想使用选项 2 并且需要将其应用于许多目标,也许可以创建一个自定义 function,它可以无条件地应用于您想要应用该选项的所有目标。
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
.您可能希望通过 cmake 缓存设置添加一个选项来禁用此功能,并将其添加到if
中完成的检查中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.