Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

My CMakeLists.txt looks like this:

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

main.cpp is a simple hello world program.

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
  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".

The CMakeError.log file looks like this:

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.

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 I'm running 64bit Windows 7

EDIT:

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).

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
  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".
like image 274
reynman Avatar asked Mar 23 '14 01:03

reynman


People also ask

Can CMake be used for C?

In the C/C++ ecosystem, the best tool for project configuration is CMake. CMake allows you to specify the build of a project, in files named CMakeLists. txt, with a simple syntax (much simpler than writing Makefiles).

Can I use Clang on Windows?

On Windows, Clang is not self-sufficient, and is supposed to be used in combination with an other compiler: either MinGW (GCC) or MSVC. Clang is going to use the standard library (and other libraries/headers) of that compiler, since it doesn't ship with ones of its own.

Does Visual Studio use Clang?

For Windows projects, Visual Studio by default invokes Clang in clang-cl mode. It links with the Microsoft implementation of the Standard Library.


1 Answers

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. load the relevant vcvarsXX.bat file (e.g. "<Your Visual Studio location>\VC\vcvarsall.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 .
like image 192
jineff Avatar answered Sep 20 '22 16:09

jineff