使用CMake,Clang和Ninja在Windows上构建c ++项目

时间:2014-03-23 01:20:42

标签: c++ windows build clang ninja

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

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

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

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

在命令行上运行:cmake -G Ninja ..,我收到以下错误:

-- 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
  CMakeFiles\cmTryCompileExec375034429.dir\testCCompiler.c.obj


  [2/2] Linking C executable cmTryCompileExec375034429.exe


  FAILED: cmd.exe /c cd .  &&
  C:\llvm_build\RelWithDebInfo\bin\clang.exe
  CMakeFiles\cmTryCompileExec375034429.dir\testCCompiler.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 CMakeFiles\cmTryCompileExec2120850158.dir\testCCompiler.c.obj

[2/2] Linking C executable cmTryCompileExec2120850158.exe

FAILED: cmd.exe /c cd . && C:\llvm_build\RelWithDebInfo\bin\clang.exe     CMakeFiles\cmTryCompileExec2120850158.dir\testCCompiler.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传递正确的参数。

FWIW我正在运行64位Windows 7

修改

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

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

MSVC_C_ARCHITECTURE_ID

2 个答案:

答案 0 :(得分:5)

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

  1. 加载相关的vcvarsXX.bat文件(例如"<Your Visual Studio location>\VC\vcvarsall.bat" x86
  2. 将CC和CXX都设置为clang-cl(而不是clangclang++
  3. 运行cmake -G Ninja <project>
  4. 运行cmake --build .

答案 1 :(得分:3)

原来我收到的第二组错误是因为clang找不到链接器。我使用visual studio构建了clang,但当时它无法找到visual studio链接器。我所要做的就是在visual studio开发控制台中运行它。

CMake仍然认为clang是一个visual studio编译器,所以在CMakeDetermineCompilerId.cmake文件中有一行看起来像这样:

set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")

我改变它看起来像这样

if (COMPILER_ID MATCHES "MSVC")
  set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
endif()

希望这不会破坏任何其他CMake功能。