在VS 2019中使用CMake,Ninja和clang构建项目

时间:2019-03-08 20:00:09

标签: c++ visual-studio cmake clang ninja

因此,我想使用CMake和clang作为VS2019的前端来构建我的项目。我已经尝试了什么: CMakeLists.txt

cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

#(CMAKE_INSTALL_PREFIX "C:\\Program Files\\LLVM\\bin")

project(sink)

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall -Wextra -std=c++17 -pedantic")

set(SRC_DIR     "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(BUILD_DIR   "${CMAKE_CURRENT_SOURCE_DIR}/build")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BUILD_DIR}/bin")

file(GLOB SOURCES "${SRC_DIR}/*.cpp" "${SRC_DIR}/*.cc" "${SRC_DIR}/*.c")
file(GLOB HEADERS "${INCLUDE_DIR}/*.h" "${INCLUDE_DIR}/*.hpp")

add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

CMakeSettings.json

{
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64" ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "-H. -G Ninja -Bbuild -DCMAKE_CXX_COMPILER:PATH=\"C:\\Program Files\\LLVM\\bin\\clang-cl.exe\"",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": []
    }
  ]
}

我得到哪些错误

1> CMake generation started.
1> Command line: E:\VS19\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe  -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\theha\CMakeBuilds\7c084052-2033-9235-9a50-93f9ca5000be\install\x64-Debug" -DCMAKE_CXX_COMPILER:FILEPATH="E:/VS19/VC/Tools/MSVC/14.20.27404/bin/HostX86/x64/cl.exe" -DCMAKE_C_COMPILER:FILEPATH="E:/VS19/VC/Tools/MSVC/14.20.27404/bin/HostX86/x64/cl.exe" -H. -G Ninja -Bbuild -DCMAKE_CXX_COMPILER:PATH="C:\Program Files\LLVM\bin\clang-cl.exe" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_MAKE_PROGRAM="E:\VS19\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "C:\Users\theha\projects\sink"
1> Working directory: C:\Users\theha\CMakeBuilds\7c084052-2033-9235-9a50-93f9ca5000be\build\x64-Debug
1> [CMake] -- The C compiler identification is MSVC 19.20.27404.0
1> [CMake] -- The CXX compiler identification is Clang 7.0.1
1> [CMake] -- Check for working C compiler: E:/VS19/VC/Tools/MSVC/14.20.27404/bin/HostX86/x64/cl.exe
1> [CMake] -- Check for working C compiler: E:/VS19/VC/Tools/MSVC/14.20.27404/bin/HostX86/x64/cl.exe -- works
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe
1> [CMake] -- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe -- broken
1> [CMake] CMake Error at E:/VS19/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.13/Modules/CMakeTestCXXCompiler.cmake:45 (message):
1> [CMake]   The C++ compiler
1> [CMake] 
1> [CMake]     "C:/Program Files/LLVM/bin/clang-cl.exe"
1> [CMake] 
1> [CMake]   is not able to compile a simple test program.
1> [CMake] 
1> [CMake]   It fails with the following output:
1> [CMake] 
1> [CMake]     Change Dir: C:/Users/theha/CMakeBuilds/7c084052-2033-9235-9a50-93f9ca5000be/build/x64-Debug/build/CMakeFiles/CMakeTmp
1> [CMake]     
1> [CMake]     Run Build Command:"E:\VS19\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "cmTC_9e38f"
1> [CMake]     [1/2] Building CXX object CMakeFiles\cmTC_9e38f.dir\testCXXCompiler.cxx.obj
1> [CMake]     FAILED: CMakeFiles/cmTC_9e38f.dir/testCXXCompiler.cxx.obj 
1> [CMake]     C:\PROGRA~1\LLVM\bin\clang-cl.exe  /nologo -TP   /DWIN32 /D_WINDOWS /W3 /GR /EHsc  /MDd /Zi /Ob0 /Od /RTC1 /JMC /showIncludes /FoCMakeFiles\cmTC_9e38f.dir\testCXXCompiler.cxx.obj /FdCMakeFiles\cmTC_9e38f.dir\ -c testCXXCompiler.cxx
1> [CMake]     clang-cl.exe: error: no such file or directory: '/JMC'
1> [CMake]     ninja: build stopped: subcommand failed.
1> [CMake]     
1> [CMake] 
1> [CMake]   
1> [CMake] 
1> [CMake]   CMake will not be able to correctly generate this project.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   CMakeLists.txt:5 (project)
1> [CMake] -- Configuring incomplete, errors occurred!
1> [CMake] See also "C:/Users/theha/CMakeBuilds/7c084052-2033-9235-9a50-93f9ca5000be/build/x64-Debug/build/CMakeFiles/CMakeOutput.log".
1> [CMake] See also "C:/Users/theha/CMakeBuilds/7c084052-2033-9235-9a50-93f9ca5000be/build/x64-Debug/build/CMakeFiles/CMakeError.log".
1> [CMake] 
1> CMake generation finished.
1> E:\VS19\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe  -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\theha\CMakeBuilds\7c084052-2033-9235-9a50-93f9ca5000be\install\x64-Debug" -DCMAKE_CXX_COMPILER:FILEPATH="E:/VS19/VC/Tools/MSVC/14.20.27404/bin/HostX86/x64/cl.exe" -DCMAKE_C_COMPILER:FILEPATH="E:/VS19/VC/Tools/MSVC/14.20.27404/bin/HostX86/x64/cl.exe" -H. -G Ninja -Bbuild -DCMAKE_CXX_COMPILER:PATH="C:\Program Files\LLVM\bin\clang-cl.exe" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_MAKE_PROGRAM="E:\VS19\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "C:\Users\theha\projects\sink" returned with exit code: 1

那么,我该如何解决?也许我做错了什么,但我不明白实际上是什么?我也看到使用vcvarsall.bat并设置CXX和CXXFLAGS的建议,但这没有帮助。

2 个答案:

答案 0 :(得分:0)

好的,也许有人会觉得我的解决方案有用。 我如何更改 CMakeLists.txt

cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project(sink)

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall -Wextra -Xclang -std=c++17 -Xclang -pedantic")

set(SRC_DIR     "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(BUILD_DIR   "${CMAKE_CURRENT_SOURCE_DIR}/build")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BUILD_DIR}/bin")

file(GLOB SOURCES "${SRC_DIR}/*.cpp" "${SRC_DIR}/*.cc" "${SRC_DIR}/*.c")
file(GLOB HEADERS "${INCLUDE_DIR}/*.h" "${INCLUDE_DIR}/*.hpp")

add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

CMakeSettings.json

{
  "configurations": [
    {
      "name": "Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64" ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "-DCMAKE_CXX_COMPILER:PATH=\"C:\\Program Files\\LLVM\\bin\\clang-cl.exe\" -DCMAKE_CXX_FLAGS:STRING=\"/DWIN32 /D_WINDOWS /GR /EHsc\" -DCMAKE_CXX_FLAGS_DEBUG:STRING=\"/MDd /Zi /Ob0 /Od /RTC1\"",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": []
    }
  ]
}

目前所有作品。

答案 1 :(得分:0)

您的解决方案有两个问题:

  • 您正在使用“ -Bbuild”定义构建目录,该目录与"buildRoot"属性中CMakeSettings.json中定义的目录不同。例如,这会使Visual Studio行为异常。它将在“ $ {buildRoot}”中查找CMakeCache.txt,而不是在“ $ {buildRoot} / build”中查找。

  • 您正在将CMAKE_CXX_COMPILER的两个定义传递给CMake,一个使用“ cl.exe”,另一个使用“ clang-cl.exe”。

您可以通过传递以下值来解决这两个问题:

{
  "configurations": [
    {
      "name": "Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [
        "msvc_x64"
      ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "CMAKE_CXX_COMPILER",
          "type": "PATH",
          "value": "C:\\Program Files\\LLVM\\bin\\clang-cl.exe"
        },
        {
          "name": "CMAKE_C_COMPILER",
          "type": "PATH",
          "value": "C:\\Program Files\\LLVM\\bin\\clang-cl.exe"
        }
      ]
    }
  ]
}

关于与/JMC无法识别的标志传递给clang-cl.exe相关的错误,clang-cl.exe当前不支持该标志,因此您需要将此patch应用于您的本地CMake安装(位于 VS_INSTALLATION_FOLDER\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake