How to specific MSVC compiler version in cmake? - visual-studio

I am trying to build llvm with MSVC 2017 as below:
git clone --branch llvmorg-9.0.1 https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build && cd build
cmake -DLLVM_ENABLE_PROJECTS=clang -G "Visual Studio 15 2017" -A x64 -Thost=x64 ..\llvm -DCMAKE_INSTALL_PREFIX=<PATH_WHERE_LLVM_WILL_BE_INSTALLED>
cmake --build . --target INSTALL --config Release
but even I specific -G "Visual Studio 15 2017" and above is running in MSVC 2017 native prompt, it compiler version identified as MSVC 2019 as below:
>cmake -DLLVM_ENABLE_PROJECTS=clang -G "Visual Studio 15 2017" -A x64 -Thost=x64 ..\llvm -DCMAKE_INSTALL_PREFIX="C:/lib/llvm"
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.16.27045.0
-- The CXX compiler identification is MSVC 19.16.27045.0
-- The ASM compiler identification is MSVC
-- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe - 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: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done

You can see in the console output that the Visual Studio 2017 compiler was indeed selected. There is no issue. Here are the lines in question:
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe - skipped
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe - skipped

Related

CMake toolset specification seems to be ignored

I'm trying to compile instant-ngp, but there are bugs building on Windows that require you to force MSVC to 14.25 (Documented in this GitHub issue.)
I'm trying to override this with -T version=14.25 or -DCMAKE_GENERATOR_TOOLSET=version=14.25, but they just don't work.
> cmake . -B build -T 'version=14.25'
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The C compiler identification is MSVC 19.29.30146.0
-- The CXX compiler identification is MSVC 19.29.30146.0
...
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
...
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
...
And it definitely builds with the 14.29 compiler.
14.25 is installed, and I know CMake can see it because if I try a version like 14.24 I get an error message about that not being installed, but 14.25 works.
How do I make CMake respect this setting instead of instantly ignoring it?

Cross compiling for x64_arm with MSVC tools on Windows

I am using MSVC command line tools to try to compile a simple c++ based program for arm/arm64 on a x64 machine. Even after using the toolchain file, the compiler definition is not getting updated and the default compiler for host is being used. Please read further for more information on the issue.
Testcase:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
}
CMakelists.txt:
cmake_minimum_required(VERSION 3.14.3)
project(hello C CXX ASM)
enable_language(ASM)
add_executable(hello hello.cpp)
Toolchain file:
cmake_minimum_required(VERSION 3.14.3)
set(CMAKE_CROSSCOMPILING 1)
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR ARM)
set(CMAKE_SYSTEM_VERSION 10.0)
set(CMAKE_C_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe")
set(CMAKE_CXX_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe")
set(CMAKE_ASM_COMPILER "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe")
The command I used to build this:
cmake.exe -S C:\hello -B C:\hello\output_dir.cmake -DCMAKE_TOOLCHAIN_FILE=C:\hello\Toolchain.cmake
With the above configuration, I am able to build a x64 based application but not a cross compiled ARM based one. From console logs, I see that the c and c++ compiler definition is not overwritten but ASM compiler is overwritten using the toolchain file.
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.
-- The C compiler identification is MSVC 19.28.29336.0
-- The CXX compiler identification is MSVC 19.28.29336.0
-- The ASM compiler identification is MSVC
-- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/x64/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: C:/hello/output_dir.cmake
I am not sure what is missing in this configuration. Can anyone please help from their experience?
Thanks for your answers.
Using set(CMAKE_GENERATOR_PLATFORM ARM64 CACHE INTERNAL "") in my toolchain file worked for me, following are the logs picking correct compiler:
> -- Building for: Visual Studio 16 2019
> -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.
> -- The C compiler identification is MSVC 19.28.29336.0
> -- The CXX compiler identification is MSVC 19.28.29336.0
> -- The ASM compiler identification is MSVC
> -- Found assembler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm/cl.exe
> -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> - works
> -- Detecting C compiler ABI info
> -- Detecting C compiler ABI info - done
> -- Detecting C compile features
> -- Detecting C compile features - done
> -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/cl.exe
> -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual
> Studio/2019/BuildTools/VC/Tools/MSVC/14.28.29333/bin/Hostx64/arm64/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: C:/hello/output_dir.cmake

Cmake error: Could NOT find Boost (missing: Boost_INCLUDE_DIR)

I am new to Cmake and Boost. I am now working on a program using cmake and need help.
System: windows 7
Cmake version: 3.16.0-rc1
Boost version: boost_1_71_0. And boost is installed through .exe file.
Gcc version: 4.8.1(rev5, Built by MinGW-W64 project)
Visual studio: vs 2015
When I run cmake .. in build dir, no error occurs. The output is:
The C compiler identification is MSVC 19.0.24215.1
The CXX compiler identification is MSVC 19.0.24215.1
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Found Boost: C:/local/boost_1_71_0 (found version "1.71.0")
Boost_Found Success!
Found OpenCV: C:/Users/gph/opencv/binaries (found version "3.4.5")
Found OpenCV: C:/Users/gph/opencv/binaries (found version "3.4.5") found components: core highgui imgproc videoio
Configuring done
But I want to use minGW to compile, so When I rum cmake -G "MinGW Makefiles", error occured saying "Could NOT find Boost (missing: Boost_INCLUDE_DIR)". The output is:
The C compiler identification is GNU 4.8.1
The CXX compiler identification is GNU 4.8.1
Check for working C compiler: C:/MinGW/bin/gcc.exe
Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: C:/MinGW/bin/g++.exe
Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
CMake Error at C:/Users/gph/cmake/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
Could NOT find Boost (missing: Boost_INCLUDE_DIR)
Call Stack (most recent call first):
C:/Users/gph/cmake/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
C:/Users/gph/cmake/share/cmake-3.16/Modules/FindBoost.cmake:2162 (find_package_handle_standard_args)
CMakeLists.txt:34 (find_package)
Configuring incomplete, errors occurred!
See also "C:/Users/gph/Desktop/libvibe++/build/CMakeFiles/CMakeOutput.log".
How to solve the error when running cmake -G "MinGW Makefiles"? Thanks guys!
I solved this by adding sentence set(BOOST_ROOT C:/local/boost_1_71_0) before find_package(Boost REQUIRED) LOL...
But I still wonder why I need to add this.
You can also set variables BOOST_ROOT during the creation of the cmake project (or CMake options in CLion and other IDE). For example:
mkdir buildtest; cd buildtest
cmake -DBOOST_ROOT=/you_path_to_boost/boost ..
cmake --build . -- -j 4
I had a similar problem on Linux/Manjaro. Make sure you have boost installed, more specifically the boost development headers. I had the boost runtime libraries installed but not the headers. I installed those sudo pacman -S boost and it fixed the problem for me.
After I solved this problem, thanks to ToughMind here, these problems occurred to me: _Boost_COMPONENT_DEPENDENCIES and _Boost_MISSING_DEPENDENCIES. What helped me was to update to the newest cmake verison and I got the answer from here: CMake finds Boost but the imported targets not available for Boost version

OpenMP on windows with clang and cmake

I'm trying to run this cmake file on windows
cmake_minimum_required(VERSION 3.8)
set(CMAKE_C_COMPILER "C:/Program Files/LLVM/bin/clang")
set(CMAKE_CXX_COMPILER "C:/Program Files/LLVM/bin/clang++")
set(OPENMP_LIBRARIES "C:/Program Files/LLVM/lib")
set(OPENMP_INCLUDES "C:/Program Files/LLVM/include")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
message("!!!!!!!!!!!!!!!! " ${OpenMP_C_FLAGS} )
find_package(OpenMP REQUIRED)
With this command line parameters:
cmake -G"Visual Studio 15 2017" -T LLVM ..
It is strange that I get the fallowing error even if OpenMP_C_FLAGS is defined according to the output:
-- The C compiler identification is Clang 9.0.0
-- The CXX compiler identification is Clang 9.0.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/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
!!!!!!!!!!!!!!!! -fopenmp=libomp -Wno-unused-command-line-argument
CMake Error at C:/Program Files/CMake/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS)
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.13/Modules/FindOpenMP.cmake:473 (find_package_handle_standard_args)
CMakeLists.txt:25 (find_package)
-- Configuring incomplete, errors occurred!
If I run cmake without -T LLVM flag, then cmake FindOpenMP.cmake runs fine (uses windows compiler). clang doesn't support openmp on windows ?

cmake fails to pick up clang++ and always use visual studio instead on Windows

I have visual studio 2017 and clang both installed in a 64-bit Windows 10 PC.
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
I have tried running cmake with clang using:
-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
but cmake always pick up visual studio instead of clang as the c++ compiler.
-- Building for: Visual Studio 15 2017
-- The C compiler identification is MSVC 19.13.26131.1
-- The CXX compiler identification is MSVC 19.13.26131.
Is there a way to ONLY use clang as my c++ compiler in cmake without other tools like visual studio?
If you wish to use VisualStudio 201x with clang/c2 compiler, forgot about linux/mac approach (it works for mingw/msys etc.). There is no need to use these CMAKE_C??_COMPILER variables.
Try e.g.
cd build
cmake -G"Visual Studio 15 2017" -T "v141_clang_c2" <SRC_DIR>
I list of available toolsets can be observed when you open any C++ project and then look to Propertiese menu -> General: "Platform toolsets" combobox-list
Or using LLVM snapshot build the page define toolset names to be used in above command e.g. LLVM-vs2013.
sample output:
C:\work\build>cmake -G"Visual Studio 15 2017" -T"v141_clang_c2" <SRC>
-- The C compiler identification is Clang 3.8.0
-- The CXX compiler identification is Clang 3.8.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/ClangC2/14.10.25903/bin/HostX86/clang.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/ClangC2/14.10.25903/bin/HostX86/clang.exe -- works
-- Detecting C compiler ABI info
....

Resources