cmake and clang++ windows build instead of MSVC - cmake

I am trying to create a build environment using cmake and i have both MSVC and clang installed on my system.
When I run cmake with the default settings it detectts MSVC as my cxx compiler.
However, I would like to use clang++ instead.
I tried specifying clang like this: cmake -DCMAKE_CXX_COMPILER=clang++
but even after that it still uses MSVC..
How can I tell it to use clang instead?

You should provide the complete path of the clang compiler. E.g.,
cmake -DCMAKE_CXX_COMPILER=/path/to/clang++ -DCMAKE_C_COMPILER=/path/to/clang

First of all you need to ensure that CLANG is visible through a regular command prompt window. If not then you need to put the CLANG installed directory path to your PATH environment variable.
In my case I had clang-cl installed through Visual Studio but it was still not visible through a regular command prompt window.
Regular command prompt window output:
>clang-cl -v
'clang++' is not recognized as an internal or external command,
operable program or batch file.
Developer command prompt window output:
>clang-cl -v
clang version 12.0.0
Target: i686-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\Llvm\bin
Moreover, I simply use the following command: cmake -T ClangCL to generate the project on a Windows 10 PC with Visual Studio 2019 Professional Edition.
This is the output I get:
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
-- The C compiler identification is Clang 12.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 12.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang-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/2019/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
I have tested the same on another Windows 10 PC with Visual Studio 2022 Community Edition and it works fine too.

Related

How to use cmake on windows with vs2019 and custom installed llvm?

I found someone mentioned to use such command line to use cmake with vs2019 and llvm toolset:
cmake -G "Visual Studio 2019" -T ClangCL
However, this would use the llvm version, which is 11.0 for now, was installed by the visual studio instanller.
I want to use a new version, thus 12.0, of llvm which I already installed somewhere else but I don't know how to make cmake and vs to use that.
UPDATE:
What I tried to set the compiler via command line:
E:\my_proj>cmake -G "Visual Studio 16 2019" -A x64 -T "ClangCL" -DCMAKE_C_COMPILER="C:\Program Files\LLVM\bin\clang.exe" -DCMAKE_CXX_COMPILER="C:\Program Files\LLVM\bin\clang++.exe" .
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is Clang 11.0.0 with MSVC-like command-line
-- The CXX compiler identification is Clang 11.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang-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/2019/Professional/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
As you could see the cmake picked the clang from the vs installed location and ignored the command line.
As described below, setting CMAKE flags explicitly has no effect when using VS generator. Thus choosing the default compiler installed by Visual Studio installer.
The VS generator produces .vcxproj files and uses MSBuild to actually
drive the build. The values of CMAKE_{C,CXX}_COMPILER are computed
automatically from the compiler MSBuild chooses so setting them has no
effect.

Qt-Cretor + CMake insists on using MSVC when I want to use MinGW

I am trying to build a CMake project with MinGW. I have installed MinGW 8.1 through the Qt installer. The CMakeList is unchanged from how Qt Creator created it when creating the project.
When running CMake, I can see how Qt Creator passes the compiler and linker's paths to CMake and how CMake completely ignores them.
Führe C:\Program Files\CMake\bin\cmake.exe -S [snip] -B [snip] "-DCMAKE_BUILD_TYPE:String=Debug" "-DQT_QMAKE_EXECUTABLE:STRING=C:/Qt/5.15.1/mingw81_64/bin/qmake.exe" "-DCMAKE_PREFIX_PATH:STRING=C:/Qt/5.15.1/mingw81_64" "-DCMAKE_C_COMPILER:STRING=C:/Qt/Tools/mingw810_64/bin/gcc.exe" "-DCMAKE_CXX_COMPILER:STRING=C:/Qt/Tools/mingw810_64/bin/g++.exe" in [snip] aus.
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19041.
-- The CXX compiler identification is MSVC 19.24.28314.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/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_BUILD_TYPE
CMAKE_C_COMPILER
QT_QMAKE_EXECUTABLE
Here I am using a system install of CMake, but the output is unchanged when using the CMake installed with Qt Creator. The CMake generator for the selected kit is set to MinGW Makefiles in the Qt Creator settings. Running CMake manually, without Qt Creator, allows me to configure the project. (although it still doesn't compile because of a linker error, that I expect to be independent of this problem)
It seems there is a caching issue. The caching is done by CMake (CMakeCache.txt) and by QtCreator (CMakeLists.txt.user) itself.
Maybe this helps:
Close QtCreator (by closing QtCreator the CMakeLists.txt.user file is saved or generated if the file don't exist)
delete QtCreator's CMakeLists.txt.user
the build folder
Open the project again and select the desired kit.

Can't tell cmake to use vcpkg for library link

I'm using a Cmake for a cross-platform project that use glibmm. Under Linux, the project build successfully without any problem (and works fine when executed).
But with Windows 10, Visual Studio 2019 (pro), I can't find how to build the project. I installed glibmm with vcpkg ("vcpkg install glibmm:x64-windows" and "vcpkg install glibmm:x86-windows")
I add to my cmake command the parameter -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake
In the main CMakeLists.txt file, the following lines do not do their job.
find_package(PkgConfig REQUIRED)
pkg_check_modules(glibmm REQUIRED glibmm-2.4)
cmake -S D:\MachDrivetoolkit\libMachDrive -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake -DPKG_CONFIG_EXECUTABLE:STRING="D:\vcpkg\downloads\tools\msys2\c809757c94447846\mingw32\bin\pkg-config.exe" -B D:\MachDrivetoolkit\libMachDriveVS
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19041.
-- The CXX compiler identification is MSVC 19.28.29334.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.28.29333/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: D:\vcpkg\downloads\tools\msys2\c809757c94447846\mingw32\bin\pkg-config.exe (found version "0.29.2")
-- Checking for module 'glibmm-2.4'
-- No package 'glibmm-2.4' found
CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.18/Modules/FindPkgConfig.cmake:545 (message):
A required package was not found
Call Stack (most recent call first):
C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.18/Modules/FindPkgConfig.cmake:733 (_pkg_check_modules_internal)
libipc/CMakeLists.txt:37 (pkg_check_modules)
Then I modify the name of the glibmm library in the script to match the name of the installed file
find_package(PkgConfig REQUIRED)
pkg_check_modules(glibmm REQUIRED glibmm)
but it fail the same.
What parameters or what instruction should I use to link glibmm to my project ?

Conan CMake and Embarcadero integration

I am trying to make Conan with CMake run with Embarcadero Tokyo 10.2.3 compiler. I can do CMake with Embarcadero following it's tutorial. But issue seems to happen with Conan. So I do the conan install. Then I do cmake .. -DCMAKE_CXX_COMPILER="bcc32c.exe" -DCMAKE_C_COMPILER="bcc32c.exe" -DCMAKE_VERBOSE_MAKEFILE=1 -G Ninja. I get the error
Incorrect 'Visual Studio'. Toolset specifies compiler as 'MSVC' but CMake
detected 'Embarcadero'
The default conan profile shows compiler=Visual Studio which I believe is where the issue originates from. But dont know how to change it to Embarcadero (does not recognize) or if changing the default profile is the way to go. My CMakeLists.txt file looks like this (mostly default)
project(Timer)
cmake_minimum_required(VERSION 3.11)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(timer timer.cpp)
target_link_libraries(timer ${CONAN_LIBS})
The full cmake output is below:
-- The C compiler identification is Embarcadero 7.30.36015
-- The CXX compiler identification is Embarcadero 7.30.36015
-- Check for working C compiler: C:/Program Files (x86)/Embarcadero/Studio/19.0/bin/bcc32c.exe
-- Check for working C compiler: C:/Program Files (x86)/Embarcadero/Studio/19.0/bin/bcc32c.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Embarcadero/Studio/19.0/bin/bcc32c.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Embarcadero/Studio/19.0/bin/bcc32c.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Current conanbuildinfo.cmake directory: C:/Mathew Joy/Source/mytimer/build
CMake Error at build/conanbuildinfo.cmake:579 (message):
**Incorrect 'Visual Studio'. Toolset specifies compiler as 'MSVC' but CMake
detected 'Embarcadero'**
Call Stack (most recent call first):
build/conanbuildinfo.cmake:237 (conan_check_compiler)
CMakeLists.txt:5 (conan_basic_setup)
You need to customize the default settings.yml file which can be found in <userhome>/.conan/settings.yml
This file has defined some of the most popular compilers, you might add something like:
compiler:
sun-cc:
...
gcc:
version: ["4.1", "4.4", "4.5", "4.6", "4.7", "4.8", "4.9",
Visual Studio:
runtime: [MD, MT, MTd, MDd]
version: ["8", "9", "10", "11", "12", "14", "15"]
...
Embarcadero:
version: ["V1", "V2"...]
This file can be shared and synchronized with the team and used in CI machines with the conan config install command.
Then you can add the values for convenience to your default profile, instead of the Visual Studio values.
It is likely that most of the logic that the built-in build helpers, like CMake are providing, would be limited for this compiler, as it doesn't recognize it. You can run your logic to call the build system directly if that is the case:
def build(self):
self.run("cmake . -G ....")
self.run("cmake --build . --config Release")
If you want to correctly build and create packages for different configurations, then you should parameterize the above using the settings and options, as necessary.
All of this assume that you will be creating and depending on your own packages, as the existing packages in the public conan-center repository will be for the popular compilers, so the binaries will be incompatible, and the recipes most likely won't implement building from sources with Embarcadero.
Visual Studio 2019 C++ is much better than Embarcadero. I have used both Tokyo and the Rio version, and there is no comparison. In addition you can download the Managed C++, which is called C++ CLR (not CLI), which will allow you to integrate .NET forms/consoles with native C++ applications.
Visual Studio 2019 C++ works well when creating Xamarin IOS libraries, which can easily be integrated with C# for Xamarin Forms. Xamarin Forms works for both Android and IOS (Xamarin).
In July 2019, Embarcadero finally added 64 bit Mac OS operating system, and MAC has been 64 bit since 2008.
That should tell you something right there.

How to build a library using cmake in windows

I've tried to build a library (called libiec61850) using cmake in windows. I have installed cmake and configured the "path" environment with the directory where I have installed the cmake. Then,
1-I created a subfolder called "build" in my library's directory
2- I copy the subfolder directory in the command line and type
cmake -G "Visual Studio 14 2015" and got many errors as follows:
- la génération a démarré
- echec de la build
- Cmakeoutput.log : "The system is: Windows - 6.1.7601 - AMD64"
See the attached file.
Please, you help will be precious.error_cmake_windows
I just looked up the library and tried it out myself, and I got it to work.
I normally use NMake to compile Visual Studios programs. If you'd like to use the same, you need to make sure that the MSVC environment is set up properly before using it. I normally run vcvarsall.bat in cmd first like so:
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat
You could do the same wherever your vcvarsall.bat file is in your Visual Studio 2015 folders (I used Visual Studio 2017, but it should work fine with Visual Studio 2015 as well).
Next, these were the commands that I used and their respective outputs:
D:\Users\patrick\src\vs2017>cd libiec61850
D:\Users\patrick\src\vs2017\libiec61850>mkdir build
D:\Users\patrick\src\vs2017\libiec61850>cd build
D:\Users\patrick\src\vs2017\libiec61850\build>cmake .. -G "NMake Makefiles"
-- The C compiler identification is MSVC 19.11.25547.0
-- The CXX compiler identification is MSVC 19.11.25547.0
-- Check for working C compiler: D:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503
/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: D:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503
/bin/Hostx64/x64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: D:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.255
03/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: D:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.255
03/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
-- Looking for clock_gettime in rt
-- Looking for clock_gettime in rt - not found
-- Check if the system is big endian
-- Searching 16 bit integer
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of unsigned short
-- Check size of unsigned short - done
-- Using unsigned short
-- Check if the system is big endian - little endian
server-example-logging: sqlite not found
-- Generating RC file : D:/Users/patrick/src/vs2017/libiec61850/build/src/version.rc
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Failed
-- Performing Test COMPILER_HAS_DEPRECATED
-- Performing Test COMPILER_HAS_DEPRECATED - Success
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Users/patrick/src/vs2017/libiec61850/build
D:\Users\patrick\src\vs2017\libiec61850\build>nmake
Microsoft (R) Program Maintenance Utility Version 14.11.25547.0
Copyright (C) Microsoft Corporation. All rights reserved.
Scanning dependencies of target iec61850
[ 0%] Building CXX object src/CMakeFiles/iec61850.dir/common/string_map.c.obj
string_map.c
It then continued compiling.
Try all of that out. If you make a build directory, you probably need to 'cd' into it, and then run cmake with a '..' to indicate that the CMakeLists.txt directory is one directory up. Let me know if you can't use NMake or if it doesn't work.