用CMake,Clang和Ninja在Windows上构buildc ++项目

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

我的CMakeLists.txt如下所示:

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

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

在命令行我运行这个: 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来testingclang。 我不知道如何告诉cmake通过正确的参数。

FWIW我正在运行64位Windows 7

编辑:

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

我删除了设置MSVC_C_ARCHITECTURE_ID CMakeDetermineCompilerId.cmake中的行,并在重试后,我得到以下错误:

 -- 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 CMakeFiles\cmTryCompileExec2815594422.dir\testCCompiler.c.obj [2/2] Linking C executable cmTryCompileExec2815594422.exe FAILED: cmd.exe /c cd . && C:\llvm_build\RelWithDebInfo\bin\clang.exe CMakeFiles\cmTryCompileExec2815594422.dir\testCCompiler.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". 

Solutions Collecting From Web of "用CMake,Clang和Ninja在Windows上构buildc ++项目"

发现我收到的第二个错误是因为铿锵找不到链接器。 我已经用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功能。

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

  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 .