设置诊断:插入符号来自 CMakeLists.txt

set diagnostics:caret from CMakeLists.txt

本文关键字:CMakeLists txt 符号 诊断 插入 设置      更新时间:2023-10-16

我想使用Visual Studio 2017中的新的(更好的(诊断信息。

要立即为我的所有项目启用它,我想从我的 CMakeList 声明此标志.txt

我试过了

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /diagnostics:caret")

但是在编译时出现一个错误,指出/diagnostics:classic(这是默认值(与/diagnostics:caret 不兼容

有没有办法使用 cmake 覆盖默认值?

你只需要知道 CMake 尚未正式支持的 VS 编译器选项最终将位于:

Properties/C/C++/Command Line/Additional Options

这就是为什么你会得到

cl : Command line error D8016: '/diagnostics:classic' and '/diagnostics:caret' 
command-line options are incompatible

但是,您可以使用新的VS_USER_PROPS目标属性(版本>= 3.8(全局提供cl选项。

这是一个工作示例:

CMakeList.txt

cmake_minimum_required(VERSION 3.0)
project(VSAnyFlag)
file(WRITE main.cpp "int main() { return 0; }")
add_executable(${PROJECT_NAME} main.cpp)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.Cpp.user.props" [=[
<?xml version="1.0" encoding="utf-8"?> 
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
<ClCompile>
<DiagnosticsFormat>Caret</DiagnosticsFormat>
</ClCompile>
</ItemDefinitionGroup>
</Project>
]=])
set_target_properties(
${PROJECT_NAME}
PROPERTIES
VS_USER_PROPS "${PROJECT_NAME}.Cpp.user.props"
)    

参考

  • 使用 CMake 添加视觉对象C++属性表