How to set compiler options with CMake in Visual Studio 2017

后端 未结 3 1698
时光取名叫无心
时光取名叫无心 2020-12-28 15:13

Visual Studio 2017 comes with full CMake integration. To learn about this combination, I was starting with this basic sample:

# CMakeLists.txt
cmake_minimum_         


        
3条回答
  •  感情败类
    2020-12-28 15:52

    Turning my comment into an answer

    CMake does come with some compiler switches preset. For visual studio those are mainly standard link libraries, warning levels, optimization levels, exception handling, debug information and platform specific defines.

    What you now have to differentiate when you want to change a CMake generated compiler settings are the following use cases:

    1. Additional compiler flags CMake does not define vs. changing CMake's preset settings
    2. Project default settings vs. project user defined settings

    So let's discuss common solutions for those cases.


    User changes/adds to Project/CMake Compiler Flags Defaults

    The standard way would be to modify the cached compiler flags variables by using tools shipped with CMake like cmake-gui and ccmake.

    To achieve this in Visual Studio you would have to:

    • CMake / Cache / View CMakeCache
    • Manually change e.g. CMAKE_CXX_FLAGS to /Wall

      CMakeCache.txt

      //Flags used by the compiler during all build types.
      CMAKE_CXX_FLAGS:STRING= /DWIN32 /D_WINDOWS /Wall /GR /EHsc
      
    • CMake / Cache / Generate


    Or you preset the CMAKE_CXX_FLAGS cache variable via a CMakeSettings.json file:

    • CMake / Change CMake Settings

      Force the cache entry with -DCMAKE_CXX_FLAGS:STRING=... in cmakeCommandArgs

      CMakeSettings.json

      {
          // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
          "configurations": [
              {
                  "name": "x86-Debug (all warnings)",
                  "generator": "Visual Studio 15 2017",
                  "configurationType": "Debug",
                  "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
                  "cmakeCommandArgs": "-DCMAKE_CXX_FLAGS:STRING=\"/DWIN32 /D_WINDOWS /Wall /GR /EHsc\"",
                  "buildCommandArgs": "-m -v:minimal"
              }
          ]
      }
      
    • If you deliver this CMakeSettings.json file with your CMake project it gets permanent


    Project changes to CMake Compiler Flags Defaults

    If you want to keep most of CMake's compiler flags in place, @sakra's answer is definitely the way to go.

    For my VS projects I've put the CXX flag settings into a toolchain file coming with the project itself. Mainly to freeze those settings and don't have a dependency the CMake version used or any environment variables set.

    Taking the example from above that would look like:

    VS2017Toolchain.cmake

    set(CMAKE_CXX_FLAGS "/DWIN32 /D_WINDOWS /Wall /GR /EHsc" CACHE INTERNAL "")
    

    CMakeSettings.json

        {
            // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
            "configurations": [
                {
                    "name": "x86-Debug (all warnings)",
                    "generator": "Visual Studio 15 2017",
                    "configurationType": "Debug",
                    "buildRoot": "${env.LOCALAPPDATA}\\CMakeBuild\\${workspaceHash}\\build\\${name}",
                    "cmakeCommandArgs": "-DCMAKE_TOOLCHAIN_FILE:FILEPATH=\"${projectDir}\\VS2017Toolchain.cmake\"",
                    "buildCommandArgs": "-m -v:minimal"
                }
            ]
        }
    

    References

    • Visual C++ Team Blog: CMake support in Visual Studio – the Visual Studio 2017 RC update
    • set diagnostics:caret from CMakeLists.txt
    • Is Cmake set variable recursive?
    • Passing compiler options cmake

提交回复
热议问题