set diagnostics:caret from CMakeLists.txt

后端 未结 1 1847
轮回少年
轮回少年 2021-01-15 07:40

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 fro

相关标签:
1条回答
  • 2021-01-15 08:25

    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

    • Add Visual C++ property sheets using CMake
    0 讨论(0)
提交回复
热议问题