简体   繁体   English

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

[英]Build project using CMake, Ninja and clang for VS 2019

So, I want to build my project using CMake and clang as a frontend for VS2019. 因此,我想使用CMake和clang作为VS2019的前端来构建我的项目。 What I already try: CMakeLists.txt 我已经尝试了什么: 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 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": []
    }
  ]
}

And which errors I get: 以及我得到哪些错误

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

So, how I can fix that? 那么,我该如何解决呢? Maybe I'm doing something wrong but I cannot understand what actually? 也许我做错了什么,但我不明白实际上是什么? I also see advices with using vcvarsall.bat and set CXX and CXXFLAGS but it isn't help. 我也看到使用vcvarsall.bat并设置CXX和CXXFLAGS的建议,但没有帮助。

Ok, maybe someone will find my solution useful. 好的,也许有人会觉得我的解决方案有用。 How I change my CMakeLists.txt : 我如何更改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})

And CMakeSettings.json : 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": []
    }
  ]
}

For now all works. 目前所有作品。

There are two problems with your solution: 您的解决方案有两个问题:

  • You are defining a build directory with "-Bbuild", which is different than the one defined in CMakeSettings.json in the "buildRoot" property. 您正在使用“ -Bbuild”定义一个构建目录,该目录与"buildRoot"属性中CMakeSettings.json中定义的目录不同。 This would make Visual Studio misbehave, as eg it will look for CMakeCache.txt in the '${buildRoot}', not in '${buildRoot}/build'. 这会使Visual Studio行为异常,例如,它将在'$ {buildRoot}'中而不是在'$ {buildRoot} / build'中查找CMakeCache.txt。

  • You are passing two definitions of CMAKE_CXX_COMPILER to CMake, one with "cl.exe", another with "clang-cl.exe". 您正在将CMAKE_CXX_COMPILER的两个定义传递给CMake,一个带有“ cl.exe”,另一个带有“ clang-cl.exe”。

You can solve both problems by passing these values instead: 您可以通过传递以下值来解决这两个问题:

{
  "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"
        }
      ]
    }
  ]
}

Regarding the error related to /JMC unrecognized flag being passed to clang-cl.exe, that flag is not currently supported by clang-cl.exe, so you need to apply this patch to your local CMake installation (located under VS_INSTALLATION_FOLDER\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\CMake ) 关于传递给clang-cl.exe的/JMC无法识别标志相关的错误,clang-cl.exe当前不支持该标志,因此您需要将此补丁应用到本地CMake安装(位于VS_INSTALLATION_FOLDER\\Common7\\IDE\\CommonExtensions\\Microsoft\\CMake\\CMake

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM