set diagnostics:caret from CMakeLists.txt

2019-02-19 12:16发布

I would like to use the new (and better) diagnostic information from visual studio 2017.

To have it enabled to all my project at once I want to declare this flag from my CMakeLists.txt

I tried

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

But when compiling there is an error saying that /diagnostics:classic (which is the default value) is not compatible with /diagnostics:caret

Is there a way to override the default value using cmake ?

1条回答
萌系小妹纸
2楼-- · 2019-02-19 12:54

You just have to know that VS compiler options that CMake does not yet officially support will end up under:

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

That's why you get

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

But you can give cl options globally with the new VS_USER_PROPS target property (version >= 3.8).

Here is a working example:

CMakeLists.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"
)    

Reference

查看更多
登录 后发表回答