使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目

Building c++ project on Windows with CMake, Clang and Ninja(使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目)

本文介绍了使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在 Windows 上安装了 cmake、clang 和 ninja.我正在尝试使用 CMake 生成 ninja 构建文件来编译一个非常简单的 hello world 程序.

I currently have cmake, clang and ninja installed on windows. I am trying to use CMake to generate a ninja build file to compile a very simple hello world program.

我的 CMakeLists.txt 看起来像这样:

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 2.8)
project(test_project)
add_executable(main main.cpp)

main.cpp 是一个简单的 hello world 程序.

main.cpp is a simple hello world program.

在命令行上我运行这个:cmake -G Ninja .. 并且我得到以下错误:

On the command line I run this: cmake -G Ninja .. and I get the following errors:

-- The C compiler identification is Clang 3.5.0
clang.exe: error: no such file or directory: '/nologo'
clang.exe: error: no such file or directory: '/showIncludes'
-- The CXX compiler identification is Clang 3.5.0
clang.exe: error: no such file or directory: '/nologo'
clang.exe: error: no such file or directory: '/showIncludes'
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler "C:/llvm_build/RelWithDebInfo/bin/clang.exe" is
  not able to compile a simple test program.

  It fails with the following output:

   Change Dir: C:/test_proj/build/CMakeFiles/CMakeTmp



  Run Build Command:C:/ninja/ninja.exe cmTryCompileExec375034429

  [1/2] Building C object
  CMakeFilescmTryCompileExec375034429.dir	estCCompiler.c.obj


  [2/2] Linking C executable cmTryCompileExec375034429.exe


  FAILED: cmd.exe /c cd .  &&
  C:llvm_buildRelWithDebInfoinclang.exe
  CMakeFilescmTryCompileExec375034429.dir	estCCompiler.c.obj -o
  cmTryCompileExec375034429.exe && cd .


  clang.exe: error: unable to execute command: program not executable



  clang.exe: error: linker command failed with exit code 1 (use -v to see
  invocation)



  ninja: build stopped: subcommand failed.






  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "C:/test_proj/build/CMakeFiles/CMakeOutput.log".
See also "C:/test_proj/build/CMakeFiles/CMakeError.log".

CMakeError.log 文件如下所示:

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: C:/llvm_build/RelWithDebInfo/bin/clang.exe 
Build flags: 
Id flags: 

The output was:
1
clang.exe: error: unable to execute command: program not executable
clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: C:/llvm_build/RelWithDebInfo/bin/clang++.exe 
Build flags: 
Id flags: 

The output was:
1
clang++.exe: error: unable to execute command: program not executable
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)


Determining if the C compiler works failed with the following output:
Change Dir: C:/test_proj/build/CMakeFiles/CMakeTmp

Run Build Command:C:/ninja/ninja.exe cmTryCompileExec2120850158
[1/2] Building C object CMakeFilescmTryCompileExec2120850158.dir	estCCompiler.c.obj

[2/2] Linking C executable cmTryCompileExec2120850158.exe

FAILED: cmd.exe /c cd . && C:llvm_buildRelWithDebInfoinclang.exe     CMakeFilescmTryCompileExec2120850158.dir	estCCompiler.c.obj  -o cmTryCompileExec2120850158.exe   && cd .

clang.exe: error: unable to execute command: program not executable


clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)


ninja: build stopped: subcommand failed.

看来 cmake 正在尝试使用 Windows 选项 /nologo/showIncludes 来测试 clang.我不知道如何告诉 cmake 传递正确的参数.

It appears that cmake is trying to test clang with windows options /nologo and /showIncludes. I cannot figure out how to tell cmake to pass the proper arguments.

FWIW 我运行的是 64 位 Windows 7

FWIW I'm running 64bit Windows 7

所以我查看了内置的 cmake 文件,我发现 CMakeClDeps.cmake 文件是添加 /nologo/showIncludes 选项的罪魁祸首.看来,如果我将 Clang 设置为编译器,那么 cmake 认为 Visual Studio 是编译器(它将 MSVC_C_ARCHITECTURE_ID 设置为 x86).

So I looked through the built in cmake files and I found that the CMakeClDeps.cmake file was the culprit for adding the /nologo /showIncludes options. It appears that if I set Clang as the compiler then cmake thinks that visual studio is the compiler (it sets MSVC_C_ARCHITECTURE_ID to x86).

我删除了 CMakeDetermineCompilerId.cmake 中设置 MSVC_C_ARCHITECTURE_ID 的行,再次尝试后,我收到以下错误:

I removed the line in CMakeDetermineCompilerId.cmake that sets MSVC_C_ARCHITECTURE_ID and after trying again I get the following errors:

-- The C compiler identification is Clang 3.5.0
-- The CXX compiler identification is Clang 3.5.0
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler "C:/llvm_build/RelWithDebInfo/bin/clang.exe" is
  not able to compile a simple test program.

  It fails with the following output:

   Change Dir: C:/test_proj/build/CMakeFiles/CMakeTmp



  Run Build Command:C:/ninja/ninja.exe cmTryCompileExec2815594422

  [1/2] Building C object
  CMakeFilescmTryCompileExec2815594422.dir	estCCompiler.c.obj


  [2/2] Linking C executable cmTryCompileExec2815594422.exe


  FAILED: cmd.exe /c cd .  &&
  C:llvm_buildRelWithDebInfoinclang.exe
  CMakeFilescmTryCompileExec2815594422.dir	estCCompiler.c.obj -o
  cmTryCompileExec2815594422.exe && cd .


  clang.exe: error: unable to execute command: program not executable



  clang.exe: error: linker command failed with exit code 1 (use -v to see
  invocation)



  ninja: build stopped: subcommand failed.






  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "C:/test_proj/build/CMakeFiles/CMakeOutput.log".
See also "C:/test_proj/build/CMakeFiles/CMakeError.log".

推荐答案

不知道它是否有帮助,但我遇到了同样的错误.现在我可以在 Windows 上使用 clang(3.7.1)/ninja(1.6)/cmake(3.4.1) 进行编译,在构建目录中执行以下操作:

Don't know if it can be helpful but I had the same error. Now I can compile with clang(3.7.1)/ninja(1.6)/cmake(3.4.1) on Windows performing the following actions in a build directory:

  1. 加载相关的 vcvarsXX.bat 文件(例如 "VCvcvarsall.bat" x86)
  2. 将 CC 和 CXX 设置为 clang-cl(而不是 clangclang++)
  3. 运行 cmake -G Ninja
  4. 运行 cmake --build .
  1. load the relevant vcvarsXX.bat file (e.g. "<Your Visual Studio location>VCvcvarsall.bat" x86)
  2. set both CC and CXX to clang-cl (instead of clang and clang++)
  3. run cmake -G Ninja <project>
  4. run cmake --build .

这篇关于使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 CMake、Clang 和 Ninja 在 Windows 上构建 C++ 项目

基础教程推荐