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

Building c++ project on Windows with CMake, Clang and Ninja

本文关键字:Windows 构建 c++ 项目 Ninja CMake Clang      更新时间:2023-10-16

我目前在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
  CMakeFilescmTryCompileExec375034429.dirtestCCompiler.c.obj

  [2/2] Linking C executable cmTryCompileExec375034429.exe

  FAILED: cmd.exe /c cd .  &&
  C:llvm_buildRelWithDebInfobinclang.exe
  CMakeFilescmTryCompileExec375034429.dirtestCCompiler.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.dirtestCCompiler.c.obj
[2/2] Linking C executable cmTryCompileExec2120850158.exe
FAILED: cmd.exe /c cd . && C:llvm_buildRelWithDebInfobinclang.exe     CMakeFilescmTryCompileExec2120850158.dirtestCCompiler.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传递正确的参数。

我正在运行64位Windows 7

编辑:

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

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

-- 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.dirtestCCompiler.c.obj

  [2/2] Linking C executable cmTryCompileExec2815594422.exe

  FAILED: cmd.exe /c cd .  &&
  C:llvm_buildRelWithDebInfobinclang.exe
  CMakeFilescmTryCompileExec2815594422.dirtestCCompiler.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)在构建目录下执行以下操作:

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

我收到的第二组错误是因为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的其他功能。