Создание проекта С++ в Windows с помощью CMake, Clang и Ninja

В настоящее время на окнах установлены cmake, clang и ninja. Я пытаюсь использовать CMake для создания файла сборки ninja для компиляции очень простой мировой программы приветствия.

My 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 пытается проверить clang с опциями windows /nologo и /showIncludes. Я не могу понять, как сказать cmake передать правильные аргументы.

FWIW Я запускаю 64-битную Windows 7

EDIT:

Итак, я просмотрел встроенные файлы cmake, и я обнаружил, что файл CMakeClDeps.cmake был виновником добавления параметров /nologo /showIncludes. Похоже, что если я установлю Clang как компилятор, тогда cmake думает, что визуальная студия является компилятором (она устанавливает 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
  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".

Ответ 1

Получается второй набор ошибок, которые я получил, потому что clang не смог найти компоновщик. Я создал clang с помощью визуальной студии, но в то время он не мог найти визуальный студийный линкер. Все, что мне нужно было сделать, это запустить его в консоли разработки визуальной студии.

CMake все еще думает, что clang - визуальный студийный компилятор, поэтому в файле CMakeDetermineCompilerId.cmake есть строка, которая выглядит так:

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

и я изменил его, чтобы выглядеть как

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

Надеюсь, это не нарушит никаких других функций CMake.

Ответ 2

Не знаю, может ли это быть полезным, но у меня была такая же ошибка. Теперь я могу скомпилировать с помощью clang (3.7.1)/ninja (1.6)/cmake (3.4.1) в Windows, выполнив следующие действия в каталоге сборки:

  • загрузите соответствующий файл vcvarsXX.bat(например, "<Your Visual Studio location>\VC\vcvarsall.bat" x86)
  • установите CC и CXX в clang-cl (вместо clang и clang++)
  • run cmake -G Ninja <project>
  • run cmake --build .