I have a C++ project that builds using CMake. I usually build on OSX but now I am trying to get a Windows version working too. I would like to use Clang on Windows for compatibility reasons.
I installed the pre-compiled Clang 3.8 binary from LLVM:
C:\Program Files\LLVM\bin\clang.exe
C:\Program Files\LLVM\bin\clang++.exe
It is also installed on my PATH:
>clang++
clang++.exe: error: no input files
I have two questions:
How do I tell CMake to use clang++ when I call cmake --build?
How can I check before building which compiler CMake is configured with?
You also need - in addition to the Clang compilers itself - an build/link environment for Windows.
The latest CMake 3.6 builds do have several integrated supported Clang build environments on Windows (e.g. Visual Studio, Cygwin; see Release Notes).
I've just run a successful test with
LLVM-3.9.0-r273898-win32.exe from http://llvm.org/builds/
cmake-3.6.0-rc4-win64-x64.msi from https://cmake.org/download/
Microsoft VS2015 Community Edition Version 14.0.23107.0
All installed to their standard paths with their bin directories in the global PATH environment.
The part you need to know is setting the right toolset with the CMake -T"LLVM-vs2014" command line option. During the configuration process CMake will let you know which compiler it has found/taken.
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(HelloWorld)
file(
WRITE main.cpp
"#include <iostream>\n"
"int main() { std::cout << \"Hello World!\" << std::endl; return 0; }"
)
add_executable(${PROJECT_NAME} main.cpp)
Windows Console
...> mkdir VS2015
...> cd VS2015
...\VS2015> cmake -G"Visual Studio 14 2015" -T"LLVM-vs2014" ..
-- The C compiler identification is Clang 3.9.0
-- The CXX compiler identification is Clang 3.9.0
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/msbuild-bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: .../VS2015
...\VS2015> cmake --build .
Microsoft (R)-Buildmodul, Version 14.0.23107.0
[...]
...\VS2015> Debug\HelloWorld.exe
Hello World!
Installation Hints
Please note that I have added LLVM to my search paths during setup:
And you can crosscheck the available "Platform Toolsets" in any VS project's property page:
References
What is the -D define to tell Cmake where to find nmake?
Linker for Clang?
Switching between GCC and Clang/LLVM using CMake
As an update for Visual Studio 2019 and 2022, you can install the clang-cl toolchain via the Visual Studio Installer and use this to generate a .sln file:
> mkdir build && cd build
> cmake .. -G "Visual Studio 16 2019" -T ClangCL -A x64
or
> mkdir build && cd build
> cmake .. -G "Visual Studio 17 2022" -T ClangCL -A x64
However a much better workflow is to simply open the folder containing the CMakeLists.txt file directly in VS and it will recognise it as a CMake project and provide the ability to set the compiler etc. directly.
The only drawback I could find to this approach is that the Visual Studio Graphics Debugger workflow doesn't work with it, which will affect you if you are developing with DirectX etc.
Follow these instructions:
Install choco if you don't have it: https://chocolatey.org/install
choco install ninja -y
choco install cmake -y
choco install llvm -y
Reset your shell so environment variables are set properly (you can check if bin folders for each are added to your Path).
Using Ninja
From PowerShell
$env:CC="C:\Program Files\LLVM\bin\clang.exe"
$env:CXX="C:\Program Files\LLVM\bin\clang++.exe"
cmake -S ./ -B ./build -G "Ninja-Multi-Config"
cmake --build ./build --config Release
From GUI
Go to your project and run:
cmake-gui .
From the upper menu select Tools/Configure and follow these settings:
Choose "Ninja Multi-Config" and Specify native compilers:
Give the path to the compilers:
Finally, run
cmake --build ./build --config Release
Using Visual Studio
Using GUI
In some folder install llvm-utils:
git clone https://github.com/zufuliu/llvm-utils.git
cd llvm-utils/VS2017
.\install.bat
Go to your project and run:
cmake-gui .
From the upper menu select Tools/Configure and follow these settings:
Choose Visual Studio 2019 and 2nd option (specify native compilers). Set
LLVM_v142 for Visual Studio 2019 and above. See here for older versions for others.
Give the path to the compilers:
Finally, run
cmake --build ./build --config Release
Related
Background
I have mutiplatform C++ project. Python is used only to download some dependencies from strange places and generate some C++ code. Python is NOT linked to any target, so I do not care what kind of platform it uses.
Problem
When configuring project for Windows 64 bit platform, python is found(find_package) without any problems. On my machine only Python 3.10.0 for Win64 is installed.
Now when I'm trying build same project for x86 (win32) it fails to configure since can't find 32 bit python.
Here is MCVE:
CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(findPythonToGenerate CXX)
find_package(Python3 REQUIRED)
configure_file(test.h.in test.h)
add_executable(foo main.cpp ${CMAKE_CURRENT_BINARY_DIR}/test.h)
target_include_directories(foo PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
main.cpp
#include <iostream>
#include "test.h"
int main()
{
std::cout << PYTHON_EXE << '\n';
return 0;
}
test.h.in
#pragma once
#define PYTHON_EXE "#Python3_EXECUTABLE#"
Now when building this for Windows 64 is just fine:
f:\mcve>cmake --version
cmake version 3.20.21032501-MSVC_2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
f:\mcve>rmdir /Q /S build32 build64
The system cannot find the file specified.
f:\mcve>cmake -S . -B build64 -A x64 -Thost=x64 -G "Visual Studio 16 2019"
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: C:/Python310/python.exe (found version "3.10.0") found components: Interpreter
-- Configuring done
-- Generating done
-- Build files have been written to: F:/mcve/build64
f:\mcve>cmake -S . -B build32 -A Win32 -Thost=x86 -G "Visual Studio 16 2019"
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30145.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter)
Call Stack (most recent call first):
C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPython/Support.cmake:3165 (find_package_handle_standard_args)
C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/FindPython3.cmake:485 (include)
CMakeLists.txt:4 (find_package)
-- Configuring incomplete, errors occurred!
See also "F:/mcve/build32/CMakeFiles/CMakeOutput.log".
Question
Can I configure cmake in some way that it give me access to python interpreter ignoring what is the current platform I'm building for? I do not want to install extra python - I do not need it.
I'm building 32 bit platform only to resolve build issues specific for that platform.
Here is similar question, but it is a different flavor, since I do not link anything with python.
Side Note
I think it was OK with older version of cmake. Recently I've updated my Visual Studio 2019 and as a result cmake too. Sadly I can't tell it for sure.
I just checked logs on build machine (which builds all platforms everyday) and on both platforms cmake 3.19 is used and same version of python and I'm sure it is 64 bit version. So looks like regression in cmake 3.20, so I raised issue.
Ok turns out that problem was type of python installation.
Some time ago I installed python 3.10 for "current user" instead for "local machine".
Apparently i didn't had to build for Win32 since that time and when I had to it happen just after MSVC upgrade.
So make sure you install python for "LOCAL MACHINE".
Trying to compile dot11decrypt on debian, installed apt-get install libtins4.0 and apt-get install libtins-dev, but cmake .. can't find package libtins, heres the error:
CMake Error at CMakeLists.txt:4 (FIND_PACKAGE):
By not providing "Findlibtins.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "libtins", but
CMake did not find one.
Could not find a package configuration file provided by "libtins" with any
of the following names:
libtinsConfig.cmake
libtins-config.cmake
Add the installation prefix of "libtins" to CMAKE_PREFIX_PATH or set
"libtins_DIR" to a directory containing one of the above files. If
"libtins" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/home/user/dot11decrypt/build/CMakeFiles/CMakeOutput.log".
cat:dot11decrypt/CMakeLists.txt
1. CMAKE_MINIMUM_REQUIRED(VERSION 2.8.1)
2. PROJECT(dot11decrypt)
3.
4. FIND_PACKAGE(libtins REQUIRED)
5.
6. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
7.
8. ADD_EXECUTABLE(dot11decrypt dot11decrypt.cpp)
9. TARGET_LINK_LIBRARIES(dot11decrypt ${LIBTINS_LIBRARIES} pthread)
find / -name Findlibtins.cmake or libtinsConfig.cmake brings no result.
I found those files on internet but still no success, i think i have to install something but don't know what.
I went with Tsyvarev solution.
# installing missing dependencies for libtins
apt-get install libpcap-dev libssl-dev cmake
# installing libtins
git clone https://github.com/mfontanini/libtins.git
cd libtins
mkdir build
cd build
cmake ../
make
checkinstall
Now cmake will reach dot11decrypt missing files.
# installing dot11decrypt
git clone https://github.com/mfontanini/dot11decrypt.git
cd dot11decrypt/
mkdir build
cd build/
cmake ../
make
Now you should have dot11decrypt executable.
If i would have to do it again i would try Alex Reinking solution first.
You don't need to compile from source... find_package is a very flexible configuration point! You'll just need to provide your own find module that is sufficient for the build. Fortunately, this is very simple. Here's what I did:
$ https://github.com/mfontanini/dot11decrypt
$ cd dot11decrypt
$ mkdir _patches
$ vim _patches/Findlibtins.cmake
$ cat _patches/Findlibtins.cmake
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBTINS REQUIRED libtins)
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_MODULE_PATH=$PWD/_patches
CMake Deprecation Warning at CMakeLists.txt:1 (CMAKE_MINIMUM_REQUIRED):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'libtins'
-- Found libtins, version 4.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/dot11decrypt/build
$ cmake --build build/
[ 50%] Building CXX object CMakeFiles/dot11decrypt.dir/dot11decrypt.cpp.o
/home/alex/test/dot11decrypt/dot11decrypt.cpp: In function ‘void decrypt_traffic(unique_fd, const string&, decrypter_tuple)’:
/home/alex/test/dot11decrypt/dot11decrypt.cpp:333:39: warning: ‘Tins::Sniffer::Sniffer(const string&, unsigned int, bool, const string&, bool)’ is deprecated [-Wdeprecated-declarations]
333 | Sniffer sniffer(iface, 2500, false);
| ^
In file included from /usr/include/tins/dns.h:38,
from /usr/include/tins/tins.h:33,
from /home/alex/test/dot11decrypt/dot11decrypt.cpp:25:
/usr/include/tins/sniffer.h:369:5: note: declared here
369 | TINS_DEPRECATED(Sniffer(const std::string& device, unsigned max_packet_size,
| ^~~~~~~~~~~~~~~
[100%] Linking CXX executable dot11decrypt
[100%] Built target dot11decrypt
The key here is to create a minimal Find module. This is just two lines on my system since it isn't meant to be reusable...
# _patches/Findlibtins.cmake
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBTINS REQUIRED libtins)
This just delegates the responsibility of setting the LIBTINS_LIBRARIES to pkg-config. To connect this find module to the build, I just set -DCMAKE_MODULE_PATH=$PWD/_patches at the CMake configure command line.
Also, you should definitely open a bug report with the dot11decrypt author about their broken and outdated build. Notice the strongly worded deprecation warning in the logs I posted.
I need to build my CMake based project under MSVC 2013 and MSVC 2019.
With MSVC 2019 using Ninja generator I build it successfully with following commands:
cmake -S . -B build -GNinja "-DCMAKE_BUILD_TYPE:STRING=Release"
cmake --build build --target all
On MSVC 2013 I have no Ninja available, so I tried the following:
cmake -S . -B build -DCMAKE_BUILD_TYPE:STRING=Release
cmake --build build --target all
Anyway I am getting following error and nothing is built:
MSBUILD : error MSB1009: Project file does not exist.
Switch: all.vcxproj
Any idea how to build it without ninja? (I cannot install it, since I am building it on a build server.)
In contrast to other generators (like Makefiles or Ninja) CMake does not generate an all target for Visual Studio solution but an ALL_BUILD target.
So cmake --build build --target ALL_BUILD --config Release should succeed.
On Windows 10 with VS2017 and LLVM installed
and CMAKE just takes VC compiler as default and ignores both set command in the CMakeLists.txt and command line -DCMAKE_CXX_COMPILER
I have no idea how to make it use LLVM Clang
I checked out many existing questions but they do not work for me
CMakeLists.txt
cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_COMPILER "C:\\Program Files\\LLVM\\bin\\Clang++")
project(CMakeTest1 VERSION 0.0.0 LANGUAGES CXX)
aux_source_directory(. MAINDIR)
add_executable(CMakeTest1 ${MAINDIR})
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
running command with git bash
***#DESKTOP-8KJ4J0H MINGW64 /d/CMakeTest1
$ cmake -DCMAKE_CXX_COMPILER="C:\Program Files\LLVM\bin\Clang++" .
-- Building for: Visual Studio 15 2017
-- The CXX compiler identification is MSVC 19.13.26129.0
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.13.26128/bin/Hostx86/x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/CMakeTest1
my CXX compiler identification is forced to be MSVC and cannot be changed by any method.
no idea why
EDITED:
what i wanna strongly declare is, i swear and clearly know that i CLEANED ALL GENERATED FILES form the folder and there are ONLY JUST CMakeLists.txt and main.cpp existing. and I DON'T WANT TO BE ASSUMED having old cache files unremoved
This may be due to default generator of CMake in Windows.
-- Building for: Visual Studio 15 2017
...
You can change the generator by adding -G <generator name> to the command line. In your case, I think -G 'MSYS Makefiles' may work.
This happens because CMAKE_CXX_COMPILER was inferred from your generator (The latest Visual Studio is used by default on windows), so it is cl.exe.
In order to switch to clang you should use other generator e.g. Ninja or Unix Makefiles.
If you want to use VS with clang-cl.exe, you need to set platform toolset to appropriate version by adding flag -TLLVM-vs2014.
See this instructions for more info.
I just cant figure out why I am getting this error, would anyone mind giving me some help?
I have added Cmake to the Environment variables, but am still getting an error. I'm currently a University Student trying to get SDL to work.
My machine is x64, I have Visual Studio 2017 with all C++ elements installed.
Here is the CMD error:
CMD Error
Here is the GUI error:
GUI Error
Copy - Paste CMD Version:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:1 (PROJECT):
No CMAKE_C_COMPILER could be found.
CMake Error at CMakeLists.txt:1 (PROJECT):
No CMAKE_CXX_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also "C:/Users/Chris Ross/.conan/data/zlib/1.2.8/lasote/stable/build/6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7/_build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/Chris Ross/.conan/data/zlib/1.2.8/lasote/stable/build/6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7/_build/CMakeFiles/CMakeError.log".
zlib/1.2.8#lasote/stable:
zlib/1.2.8#lasote/stable: ERROR: Package '6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7' build failed
zlib/1.2.8#lasote/stable: WARN: Build folder C:\Users\Chris Ross\.conan\data\zlib\1.2.8\lasote\stable\build\6cc50b139b9c3d27b3e9042d5f5372d327b3a9f7
ERROR: zlib/1.2.8#lasote/stable: Error in build() method, line 60
self.run('%s && cmake .. %s' % (cd_build, cmake.command_line))
ConanException: Error 1 while executing cd _build && cmake .. -G "Visual Studio 15 2017 Win64" -DCONAN_LINK_RUNTIME="/MD" -DCONAN_EXPORTED="1" -DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="15" -DCONAN_CXX_FLAGS="/MP4" -DCONAN_C_FLAGS="/MP4" -Wno-dev
INFO: Conan job finished.
INFO: Starting cmake
CMake Error: Error: generator : Visual Studio 14 2015 Win64
Does not match the generator used previously: Visual Studio 15 2017 Win64
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
INFO: CMake job finished.
Copy - Paste GUI Version:
CMAKE_CONFIGURATION_TYPES Debug;Release;MinSizeRel;RelWithDebInfo
CMAKE_INSTALL_PREFIX C:/Program Files (x86)/lab1
Configuring incomplete, errors occurred!
See also "C:/Users/Chris Ross/Desktop/lab2template/bin/CMakeFiles/CMakeOutput.log".
See also "C:/Users/Chris Ross/Desktop/lab2template/bin/CMakeFiles/CMakeError.log".
My conanfile.txt:
[requires]
SDL2/2.0.5#dotfloat/stable
SDL2_image/2.0.1#lasote/stable
SDL2_mixer/2.0.1#a_teammate/testing
SDL2_ttf/2.0.14#hilborn/stable
[options]
SDL2:shared=True
SDL2_image:shared=False
SDL2_mixer:shared=True
SDL2_ttf:shared=False
[generators]
cmake
[imports]
bin, *.dll -> ./bin # Copies all dll files from packages bin folder to my "bin" folder
lib, *.dylib* -> ./bin # Cop
Using this .bat file:
#echo off
:start
echo INFO: I am running from '%cd%'
if exist src goto changetobin
:conan
echo INFO: Starting conan
conan install --build missing
echo INFO: Conan job finished.
goto cmake
:cmake
echo INFO: Starting cmake
cmake .. -G "Visual Studio 14 2015 Win64"
echo INFO: CMake job finished.
goto end
:changetobin
echo ERROR: I'm in the wrong directory.. moving into bin
cd bin
echo INFO: Retrying..
goto start
:end
if exist bin goto quit
cd ..
goto quit
:quit
echo.
echo.
echo INFO: Conan and CMake ran successfully. Open up your solution file (listed below) to open your project.
echo.
dir /b *.sln
(I have also attempted a manual build (conan install --build missing))
"My machine is x64, I have Visual Studio 2017 with all C++ elements installed."
You're generating a solution for the wrong version of Visual Studio..
cmake .. -G "Visual Studio 14 2015 Win64"
Change that line in your batch script to:
cmake .. -G "Visual Studio 15 2017 Win64"
Then you must remove all generated files (CMakeCache.txt', 'CMakeFiles dir, etc`) and try again. Please also double check that your conan profile settings match the compiler you are using.