CMake building for Windows (clang-cl) using Ninja Generator - cmake

I am trying to build a simple application on a Windows machine using CMake as the main build tool. Once CMake is invoked on the project the is an error on configuration phase:
> cmake -H. -G Ninja -Bbuild -DCMAKE_C_COMPILER:PATH="C:\Program Files\LLVM\bin\clang-cl.exe" -DCMAKE_CXX_COMPILER:PATH="C:\Program Files\LLVM\bin\clang-cl.exe"
-- The C compiler identification is Clang 7.0.0
-- The CXX compiler identification is Clang 7.0.0
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe
-- Check for working C compiler: C:/Program Files/LLVM/bin/clang-cl.exe --broken
CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/CMakeTestCCompile
r.cmake:52 (message):
The C compiler
"C:/Program Files/LLVM/bin/clang-cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/mak/Desktop/cmake-test/build/CMakeFiles/CMakeTmp
Run Build Command:"C:/Qt/Tools/QtCreator/bin/ninja.exe" "cmTC_f5485"
[1/2] Building C object CMakeFiles\cmTC_f5485.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTC_f5485.exe
FAILED: cmTC_f5485.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_f5485.dir --manifests -- CMAKE_LINKER-NOTFOUND /nologo CMakeFiles\cmTC_f5485.dir\testCCompiler.c.obj /out:cmTC_f5485.exe /implib:cmTC_f5485.lib /pdb:cmTC_f5485.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
RC Pass 1: command "rc /foCMakeFiles\cmTC_f5485.dir/manifest.res CMakeFiles\cmTC_f5485.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the given file
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
I read a lot through the web but my problem was not solved by any proposed solution. What I found so far is a simmilar but maybe outdated solution to the same problem which did not work for me, because Ninja was not able to build the executable:
> ninja all
[1/2] Building CXX object CMakeFiles/minimal.dir/main.cpp.obj
FAILED: CMakeFiles/minimal.dir/main.cpp.obj
C:\PROGRA~1\LLVM\bin\clang-cl.exe -MD -MT CMakeFiles/minimal.dir/main.cpp.obj -MF CMakeFiles\minimal.dir\main.cpp.obj.d -o CMakeFiles/minimal.dir/main.cpp.obj -c ../main.cpp
clang-cl.exe: warning: unknown argument ignored in clang-cl: '-MF' [-Wunknown-argument]
clang-cl.exe: error: no such file or directory: 'CMakeFiles/minimal.dir/main.cpp.obj'
clang-cl.exe: error: no such file or directory: 'CMakeFiles\minimal.dir\main.cpp.obj.d'
ninja: build stopped: subcommand failed.
Before this error CMake configured properly except that all compilers ABI info detection failed - but CMake resumed without error. There are some other questions out there which did not help either.
The official documentation states it is quite simple but in fact it is not.
So: How do I build a simple C++ project using CMake with the Ninja generator and Clang as the compiler? I try to avoid the installation of Visual Studio but it would be great if generated binaries are compatible with MSVC build binaries.
Versions:
CMake 3.12.2
Ninja 1.8.2
Clang 7.0.0
Example: Here is the minial example which I am working with:
CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(minimal)
add_executable(${PROJECT_NAME} main.cpp)
main.cpp
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}

To compile with clang-cl, it is necessary to run cmake with MSVC environment loaded (use vcvarsall.bat). Otherwise it tries to use GCC compatibility options.
It is enough to install just the Build Tools.

Related

CMake for MinGW Makefiles won't create the build

I am trying to create a build of MinGW Makefiles using cmake but I get the following error when I execute the command cmake -G "MinGW Makefiles" ..\source\
I have attached a picture of my path varibles below.Path Variables. I get the following error on running the cmake -G commmand
PS C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build> cmake ..\source\ -G "MinGW Makefiles"
-- The CXX compiler identification is MSVC 19.33.31630.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCXXCompiler.cmake:63 (message):
The C++ compiler
"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeScratch/TryCompile-mhmynt
Run Build Command(s):C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_29120/fast && C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\cmTC_29120.dir\build.make CMakeFiles/cmTC_29120.dir/build
mingw32-make.exe[1]: Entering directory 'C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeScratch/TryCompile-mhmynt'
Building CXX object CMakeFiles/cmTC_29120.dir/testCXXCompiler.cxx.obj
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_cl_compile_depends --dep-file=CMakeFiles\cmTC_29120.dir\testCXXCompiler.cxx.obj.d --working-dir="C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build\CMakeFiles\CMakeScratch\TryCompile-mhmynt" --filter-prefix="Note: including file: " -- C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1433~1.316\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoCMakeFiles\cmTC_29120.dir\testCXXCompiler.cxx.obj /FdCMakeFiles\cmTC_29120.dir/ /FS -c "C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build\CMakeFiles\CMakeScratch\TryCompile-mhmynt\testCXXCompiler.cxx"
testCXXCompiler.cxx
Linking CXX executable cmTC_29120.exe
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_29120.dir\link.txt --verbose=1
"C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_29120.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1433~1.316\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\cmTC_29120.dir\objects1 /out:cmTC_29120.exe /implib:cmTC_29120.lib /pdb:"C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build\CMakeFiles\CMakeScratch\TryCompile-mhmynt\cmTC_29120.pdb" /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib
RC Pass 1: command "rc /fo CMakeFiles\cmTC_29120.dir/manifest.res CMakeFiles\cmTC_29120.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specifiedCMakeFiles\cmTC_29120.dir\build.make:99: recipe for target 'cmTC_29120.exe' failed
mingw32-make.exe[1]: *** [cmTC_29120.exe] Error -1
mingw32-make.exe[1]: Leaving directory 'C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeScratch/TryCompile-mhmynt'
Makefile:126: recipe for target 'cmTC_29120/fast' failed
mingw32-make.exe: *** [cmTC_29120/fast] Error 2
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:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeError.log".
PS C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build>
I am trying to avoid adding the compiler to be used in the cmakelist.txt file as I saw that it is a bad practice. I searched and found alternative approaches using -D flag and saving the preferred settings in cache but I am curious to know why this one works in the tutorial but not on my system.
I tinkered for a bit and realized it is necessary to give the g++ compiler as a flag to the cmake command. The default compiler here is MSVC but mingw-makefile use the g++.exe. Thus, the command cmake -G MinGW Makefiles" -D CMAKE_CXX_COMPILER=g++ ..\source\ will create a build for your project given the build repository doesn't have any existing cache, if it does, then you would need to clear it.
One thing to keep in mind is that compiler = g++ is the name of the application of the compiler in path. So you must add it to path and write the exact name without ext. here.
Additionally, I tried to run the ninja build system, which also showed the same error as the original question. Similar changes in the command fixed it as well, so cmake -G "Ninja" -D CMAKE_CXX_COMPILER=g++ ..\source\ will build your project, given you have no cache in build. Delete if you do.
I could not find out how to run ninja using msvc though, replacing g++ with the cl exe did not work.
Edit: How to use Clang-CL LLVM with VS2019 CMAKE? for the clang-msvc one.

CMake project using ifort failed. "is not able to compile a simple test program."

I can compile the project using gfortran in both Linux and Windows with no error. But for some reason I have to use ifort.exe as compiler in Windows and it failed. Anyone can help?
The detailed information is as follows:
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - failed
-- Check for working Fortran compiler: D:/Program Files/IVF/compilers_and_libraries_2018.3.210/windows/bin/intel64/ifort.exe
-- Check for working Fortran compiler: D:/Program Files/IVF/compilers_and_libraries_2018.3.210/windows/bin/intel64/ifort.exe- broken CMake Error at D:/Program Files/CMake/share/cmake-3.18/Modules/CMakeTestFortranCompiler.cmake:51 (message): The Fortran compiler
"D:/Program Files/IVF/compilers_and_libraries_2018.3.210/windows/bin/intel64/ifort.exe"
is not able to compile a simple test program. It fails with the following output:
Change Dir: C:/Users/xxx/build/CMakeFiles/CMakeTmp
Run Build Command(s):D:/PROGRA~2/MINGW-~1/X86_64~1.0-P/mingw64/bin/mingw32-make.exe cmTC_809ef/fast && mingw32-make.exe[1]: Entering directory 'C:/Users/xxx/build/CMakeFiles/CMakeTmp'
D:/PROGRA~2/MINGW-~1/X86_64~1.0-P/mingw64/bin/mingw32-make.exe -f CMakeFiles\cmTC_809ef.dir\build.make CMakeFiles/cmTC_809ef.dir/build
mingw32-make.exe[2]: Entering directory 'C:/Users/xxx/build/CMakeFiles/CMakeTmp'
Building Fortran object CMakeFiles/cmTC_809ef.dir/testFortranCompiler.f.obj
D:\PROGRA~2\IVF\COMPIL~1.210\windows\bin\intel64\ifort.exe /nologo /fpp /W1 /nologo /fpp /libs:dll /threads /Od /debug:full /dbglibs /FoCMakeFiles\cmTC_809ef.dir\testFortranCompiler.f.obj /FdCMakeFiles\cmTC_809ef.dir/ -c C:\Users xxx\build\CMakeFiles\CMakeTmp\testFortranCompiler.f
warning #31001: The dll for reading and writing the pdb (for example, mspdb110.dll) could not be found on your path. This is usually a configuration error. Compilation will continue using /Z7 instead of /Zi, but expect a similar err r when you link your program.
Linking Fortran executable cmTC_809ef.exe
"D:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_809ef.dir\link.txt --verbose=1
"D:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_809ef.dir --rc=rc --mt="" --manifests -- xilink /nologo #CMakeFiles\cmTC_809ef.dir\objects1.rsp /out:cmTC_809ef.exe /implib:cmTC_809ef.lib /pdb:C:\Users\xxx\build\CMakeFiles\CMakeTmp\cmTC_809ef.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL:YES /subsystem:console user32.lib
RC Pass 1: command "rc /fo CMakeFiles\cmTC_809ef.dir/manifest.res CMakeFiles\cmTC_809ef.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specified.mingw32-make.exe[2]: *** [CMakeFiles\cmTC_809ef.dir\build.make:107: cmTC_809ef.exe] Error -1 mingw32-
make.exe[2]: Leaving directory 'C:/Users/xxx/build/CMakeFiles/CMakeTmp'
mingw32-make.exe[1]: *** [Makefile:140: cmTC_809ef/fast] Error 2
mingw32-make.exe[1]: Leaving directory 'C:/Users/xxx/build/CMakeFiles/CMakeTmp'
The error is because that the rc.exe cannot be found. I solved the problem by adding the file path to the PATH variable.

Simple test output - CMake Error: The source directory does not exist

When I create new project in Clion, cmake cannot find CMakeTmp directory.
Run Build Command(s):/usr/bin/cmake cmTC_df723/fast && No such file or directory
OS: Linux Mint 19 (Ubuntu based)
I have tried:
reinstalling gcc (now on version 8.3.0)
reinstalling cmake (now on version 3.15.2)
unset TMP from (CMake cannot compile test program as source directory doesnt exist)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_SYSTEM_NAME Linux)
checked permissions to ClionProject subdirectories
Error output:
/usr/local/bin/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=/usr/bin/cmake -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/c++ -DCMAKE_C_COMPILER=/usr/bin/gcc -G "CodeBlocks - Unix Makefiles" /home/sacha/CLionProjects/testProject
-- The C compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/gcc
CMake Error: Generator: execution of make failed. Make command was: /usr/bin/cmake cmTC_df723/fast &&
-- Check for working C compiler: /usr/bin/gcc -- broken
CMake Error at /usr/local/share/cmake-3.15/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"/usr/bin/gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/sacha/CLionProjects/testProject/cmake-build-release-system/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/cmake cmTC_df723/fast && No such file or directory
Generator: execution of make failed. Make command was: /usr/bin/cmake cmTC_df723/fast &&
Any help or directions appreciated, thanks!
The error message
CMake Error: Generator: execution of make failed.
signals, that something wrong with CMake settings or its generator.
In the given case you have additional message
Make command was: /usr/bin/cmake cmTC_df723/fast &&
which means that cmake is run as a Make command.
This is wrong: cmake and make commands are not interchangeable.
Would you inspect the very first command line, you will find the source of that problem:
-DCMAKE_MAKE_PROGRAM=/usr/bin/cmake
Exactly it sets cmake executable for Make command. You need to fix that line (e.g. in CLion settings).

Building ParaView-Superbuild in windows 10

I'm trying to build the ParaView-Superbuild (https://gitlab.kitware.com/paraview/paraview-superbuild) on Windows 10 64x.
I cannot manage to get it to work. I just ran from error to error. Currently I'm stuck at:
CMake Error at superbuild/projects/win32/boost.cmake:5 (message):
At least Visual Studio 9.0 is required
Call Stack (most recent call first):
superbuild/cmake/SuperbuildMacros.cmake:690 (include)
superbuild/CMakeLists.txt:145 (_superbuild_discover_projects)
using Cmake 3.13.10-rc1 as generator for Ninja. I have Cygwin64 installed and I'm using the GCC compiler
The C compiler identification is GNU 7.3.0
The CXX compiler identification is GNU 7.3.0
Since I thought that the error has something to do with the compiler I tried to switch to the installed VS2017 compiler. However, the build process will then crash with
CMake Error at C:/Program Files/CMake/share/cmake-3.13/Modules
/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools
/MSVC/14.15.26726/bin/Hostx86/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/paraview-superbuild/build/CMakeFiles/CMakeTmp
Run Build Command:"C:/Ninja/ninja.exe" "cmTC_915a7"
[1/2] Building C object CMakeFiles\cmTC_915a7.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTC_915a7.exe
FAILED: cmTC_915a7.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe
--intdir=CMakeFiles\cmTC_915a7.dir --manifests -- C:\PROGRA~2\MICROS~1
\2017\ENTERP~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe /nologo
CMakeFiles\cmTC_915a7.dir\testCCompiler.c.obj /out:cmTC_915a7.exe
/implib:cmTC_915a7.lib /pdb:cmTC_915a7.pdb /version:0.0 /machine:x64
/debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib
winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib
advapi32.lib && cd ."
RC Pass 1: command "rc /foCMakeFiles\cmTC_915a7.dir/manifest.res
CMakeFiles\cmTC_915a7.dir/manifest.rc" failed (exit code 0) with the
following output:
The system cannot find the file specified
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (
I don't know how to fix the error using the GCC compiler when the VS one is not working at all. Sadly, I couldn't find any help on the Paraview website.
Has somebody managed to build it and can show me step by step how to do it?
And no, I don't want to use Linux here.
Thanks.
Here is a reliable way to build ParaView Superbuild on windows
Install Visual Studio 2013 community with all C++ related tools
Install cmake
Install git bash
Install Ninja
Install Qt 5.9.2 VS2013
Install other potential dependencies
open git bash
then :
cd C:/
mkdir pv
cd pv
git clone git#gitlab.kitware.com:paraview/paraview-superbuild.git
mv paraview-superbuild pvsb
mkdir pvsb_b
cd pvsb
git submodule update --init --recursive
Open Visual Studio Native Command X64
then :
cd C:\pv\pvsb_b
cmake-gui
configure paraview-superbuild accordingly to your need
ENABLE_qt5 and USE_SYSTEM_qt should be ON
generate
close cmake-gui
ninja

Clang and CMake on Windows

I am building a C++ project with Clang 3.8 and CMake 3.7 on Windows, but Clang emits an error in configure like below.
Determining if the CXX compiler works failed with the following output:
Change Dir: C:/tools/k.build/CMakeFiles/CMakeTmp
Run Build Command:"C:/tools/ninja.exe" "cmTC_c0004"
[1/2] Building CXX object CMakeFiles/cmTC_c0004.dir/testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_c0004.exe
FAILED: cmTC_c0004.exe
cmd.exe /C "cd . && C:\tools\llvm-3.8.1.build\Release\bin\clang++.exe CMakeFiles/cmTC_c0004.dir/testCXXCompiler.cxx.obj -o cmTC_c0004.exe -Wl,--out-implib,libcmTC_c0004.dll.a -Wl,--major-image-version,0,--minor-image-version,0 && cd ."
LINK : warning LNK4044: unrecognized option '/-out-implib'; ignored
LINK : warning LNK4044: unrecognized option '/-major-image-version'; ignored
LINK : warning LNK4044: unrecognized option '/-minor-image-version'; ignored
LINK : fatal error LNK1181: cannot open input file 'libcmTC_c0004.dll.a'
clang++.exe: error: linker command failed with exit code 1181 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Strange thing is that no file exists in CMakeFiles/CMakeTmp/. How can I get it to work?
Simply put, you are defining your toolchain wrong. CMake thinks you are using the MSVC compiler with a different path. See this question for a detailed answer. Note that you will still need MSVC or Cygwin as a build environment.