cmake switch to use gcc/clang failed and always use msvc compiler - c++

I'm on win10 and I have installed: MSVS2022 with msc++ compiler, codeblocks with Mingwin-g++ compiler and then llvm clang.
I then installed latest cmake and have a very simple CMakeLists.txt in a directory named basic with some cc files:
cmake_minimum_required(VERSION 2.8)
project(my)
add_executable(my main.cc power.cc)
I do these commands in powershell:
mkdir build
cd build
cmake ../basic.
-- Building for: Visual Studio 17 2022
CMake Deprecation Warning at CMakeLists.txt:17 (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 MSVC 19.31.31104.0
-- The CXX compiler identification is MSVC 19.31.31104.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: d:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/H
But I wish I could use g++ or clang++, so I tried several methods at the same time.
(1) I changed CMakeLists.txt to add some lines:
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
SET (CMAKE_C_COMPILER "d:\\Program Files\\LLVM\\bin\\clang.exe")
SET (CMAKE_C_FLAGS "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
SET (CMAKE_CXX_COMPILER "d:\\Program Files\\LLVM\\bin\\clang++.exe")
SET (CMAKE_CXX_FLAGS "-Wall")
SET (CMAKE_CXX_FLAGS_DEBUG "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
cmake_minimum_required(VERSION 2.8)
project(my)
add_executable(my main.cc power.cc)
I cleaned the directory under build and run cmake again. Doesn't work, still choose msvc compiler.
Then I set environmental variable in Powershell like this:
$env:CC="d:\Program Files\LLVM\bin\clang.exe"
$env:CXX="d:\Program Files\LLVM\bin\clang++.exe"
I tried again. Doesn' work. Then I used the command line option:
cmake ..\basic\ -DCMAKE_CXX_COMPILER="d:\Program Files\LLVM\bin\clang++.exe"
Doesn't work. All methods tried together, still chooses msvc.
How to solve this problem?

Related

Setting CMake compiler explicitly to GCC causes infinite loop

I'd like to set the compiler version in CMake so I can switch between GCC and Clang just by commenting out a few lines.
Below is the top of my CMake script, with the two additional lines setting GCC as the compiler:
cmake_minimum_required(VERSION 3.4.1...3.17.2)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
project(test_project C CXX)
set (CMAKE_C_COMPILER "/usr/bin/gcc") # Adding these causes infinite loop
set (CMAKE_CXX_COMPILER "/usr/bin/g++")
message(STATUS "Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Compiler Vers: ${CMAKE_CXX_COMPILER_VERSION}")
However, when I add the above two lines and run cmake .. the script enters an infinite loop, outputting:
You have changed variables that require your cache to be deleted
Configure will be re-run and you may have to reset some variables
The following variables have changed:
CMAKE_C_COMPILER= /usr/bin/cc
CMAKE_CXX_COMPILER= /usr/bin/c++
CMAKE_CXX_COMPILER= /usr/bin/c++
CMAKE_C_COMPILER= /usr/bin/cc
CMAKE_CXX_COMPILER= /usr/bin/c++
CMAKE_C_COMPILER= /usr/bin/cc
I'd eventually like to have something like this:
cmake_minimum_required(VERSION 3.4.1...3.17.2)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
project(test_project C CXX)
set (CMAKE_C_COMPILER "/usr/bin/gcc")
set (CMAKE_CXX_COMPILER "/usr/bin/g++")
#set (CMAKE_C_COMPILER "/usr/bin/clang")
#set (CMAKE_CXX_COMPILER "/usr/bin/clang++")
message(STATUS "Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Compiler Vers: ${CMAKE_CXX_COMPILER_VERSION}")
Anyone know what's wrong?
It seems to be an ancient CMake bug, where the workaround proposed is just putting the variables before the project command:
cmake_minimum_required(VERSION 3.17.2)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/g++")
project(test_project C CXX)
...
EDIT:
As other mentioned, this is not actually a bug, but design decision: project command selects compiler based on the provided settings, and it never should be altered after the command comes into play.
BTW, these variables are not for the set by the project. This is done intentionally so a user may choose the compiler he wants to build the project. The project maintainer should not decide for the user what compiler to use. Who told you that it must be only /usr/bin/*? What if GCC came from SCL (on RHEL/CentOS) where it gonna be installed to /opt/rh/devtoolset-N/... ... Please leave to users the freedom to choose what compiler to use!
These variables must be set via CLI, initial cache, toolchain file, presets, or environment variables.

cmake -D CMAKE_TOOLCHAIN_FILE doesn't work on my windows, still looking for MS compiler

I've got MSVS 2022 and clang llvm installed on win10, I'm using vscode and cmake(version 3.23.2). On powershell command line I have a directory containing several cpp files and a CMakeLists.txt, then:
mkdir build
cd build
cmake -D CMAKE_TOOLCHAIN_FILE="..\cmaketoolchain.win10clang" ..
The content of cmaketoolchain.win10clang is as below:
set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
SET (CMAKE_C_COMPILER "d:\\Program Files\\LLVM\\bin\\clang.exe")
SET (CMAKE_C_FLAGS "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
SET (CMAKE_CXX_COMPILER "d:\\Program Files\\LLVM\\bin\\clang++.exe")
SET (CMAKE_CXX_FLAGS "-Wall")
SET (CMAKE_CXX_FLAGS_DEBUG "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
Well, on running cmake it still prints:
cmake -D CMAKE_TOOLCHAIN_FILE="..\cmaketoolchain.win10clang" ..
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
-- The C compiler identification is MSVC 19.31.31104.0
-- The CXX compiler identification is MSVC 19.31.31104.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: d:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
...
And generate a bunch of vcproj files.
Did I miss anything about cmake or environmental setting? How to solve it?

Qt Creator + CMake + Clang

I am trying to use Qt Creator to work on a non-Qt C++ program, built using CMake with Clang as the compiler. Platform is Windows 10 64. All versions are most recent at time of this post. After manually downloading Jom and adding it to the path, I get this error trying to build a Hello World program:
The C compiler identification is Clang 6.0.0
The CXX compiler identification is Clang 6.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/CMakeTestCCompiler.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/Alex/AppData/Local/Temp/QtCreator-zKOojN/qtc-cmake-TXXABrTJ/CMakeFiles/CMakeTmp
Run Build Command:"jom" "/nologo" "cmTC_75f57\fast"
jom: parallel job execution disabled for Makefile
C:\apps\Jom\jom.exe -f CMakeFiles\cmTC_75f57.dir\build.make /nologo -L CMakeFiles\cmTC_75f57.dir\build
Building C object CMakeFiles/cmTC_75f57.dir/testCCompiler.c.obj
C:\PROGRA~1\LLVM\bin\clang-cl.exe #C:\Users\Alex\AppData\Local\Temp\testCCompiler.c.obj.12652.16.jom
Linking C executable cmTC_75f57.exe
"C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_75f57.dir --manifests -- llvm-ld /nologo #CMakeFiles\cmTC_75f57.dir\objects1.rsp #C:\Users\Alex\AppData\Local\Temp\cmTC_75f57.exe.12652.297.jom
RC Pass 1: command "rc /foCMakeFiles\cmTC_75f57.dir/manifest.res CMakeFiles\cmTC_75f57.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specifiedjom: C:\Users\Alex\AppData\Local\Temp\QtCreator-zKOojN\qtc-cmake-TXXABrTJ\CMakeFiles\CMakeTmp\CMakeFiles\cmTC_75f57.dir\build.make [cmTC_75f57.exe] Error 2
jom: C:\Users\Alex\AppData\Local\Temp\QtCreator-zKOojN\qtc-cmake-TXXABrTJ\CMakeFiles\CMakeTmp\Makefile [cmTC_75f57\fast] Error 2
Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
SET (CMAKE_C_COMPILER "clang-cl")
SET (CMAKE_C_FLAGS "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
SET (CMAKE_CXX_COMPILER "clang-cl")
SET (CMAKE_CXX_FLAGS "-Wall")
SET (CMAKE_CXX_FLAGS_DEBUG "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
SET (CMAKE_AR "llvm-ar")
SET (CMAKE_LINKER "llvm-ld")
SET (CMAKE_NM "llvm-nm")
SET (CMAKE_OBJDUMP "llvm-objdump")
SET (CMAKE_RANLIB "llvm-ranlib")
project(TestProg)
add_executable(${PROJECT_NAME} "main.cpp")

Cmake doesn't recognize MSVC compiler

It's the first time I encounter this error.
I am trying to clone openvr and get the hello opengl sample running, following their instructions:
cd samples
mkdir build
cd build
cmake .. -G "Visual Studio 14 2015 Win64" -DCMAKE_PREFIX_PATH=C:/Qt/5.6/msvc2013_64/lib/cmake
I get:
$ cmake .. -G "Visual Studio 14 2015 Win64" -DCMAKE_PREFIX_PATH=C:/Qt/5.6/msvc20 13_64/lib/cmake
-- 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 using: Visual Studio 14 2015 Win64
-- Check for working C compiler using: Visual Studio 14 2015 Win64 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 14 2015 Win64
-- Check for working CXX compiler using: Visual Studio 14 2015 Win64 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Compilation set for 64bits architectures.
CMake Warning at CMakeLists.txt:40 (message):
SDL x64 runtime binaries not provided on Windows.
CMake Error at CMakeLists.txt:98 (message):
Unsupported compiler.
-- Configuring incomplete, errors occurred!
See also "C:/Users/GBarbieri/Documents/Visual Studio 2015/Projects/openvr/sample s/build/CMakeFiles/CMakeOutput.log".
If I go to check line 98:
if( (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
OR (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang"))
# Better to use the prebuilt GNU preprocessor define __GNUC__,
# kept for legacy reason with the sample code.
add_definitions(-DGNUC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -include ${SHARED_SRC_DIR}/compat.h")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -pedantic -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
# Handles x86 compilation support on x64 arch.
if(${PLATFORM} MATCHES 32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32")
endif()
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Za")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /w2 /DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MP /INCREMENTAL:NO")
else()
message(FATAL_ERROR "Unsupported compiler. ") // line 98
endif()
It seems it couldn't match "MSVC".. let me do a try, let me add before this snippet of code:
message("${CMAKE_CXX_COMPILER_ID}")
if(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
message("match")
else()
message("!match")
endif()
And then it returns:
MSVC
!match
CMake Error at CMakeLists.txt:104 (message):
Unsupported compiler.
What?
Can you spot what is wrong, guys?
Ok, as I've tried in the comment, the right answer is to use:
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
instead of
if(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")

Mixing C++ and Fortran

Is there a way to use the Clang compiler while mixing C++ and Fortran?
Until now I use cmake with
project(mixing CXX Fortran)
but this triggers the use of g++.
-- The CXX compiler identification is GNU 6.2.0
CMakeLists.txt of my project with Fortran mixing:
cmake_minimum_required(VERSION 3.7.0)
project(mixing CXX Fortran)
# SET UP ROOT https://root.cern.ch/how/integrate-root-my-project-cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /opt/local/libexec/root6/etc/root/cmake)
find_package(ROOT REQUIRED COMPONENTS MATH MINUIT2)
include(${ROOT_USE_FILE})
include_directories(Experiment Theory ${ROOT_INCLUDE_DIRS})
add_executable(mixing main.cpp)
target_link_libraries(mixing ${ROOT_LIBRARIES})
Not working, because g++ cannot use the needed Clang flag -stdlib=libc++ of the ROOT library.
You can always override c/c++ compiler by changing CMAKE_<LANG>_COMPILER, where <LANG> in your case is C or CXX.
You can set your CC and CXX environment variables to override defaults for CMAKE_C_COMPILER / CMAKE_CXX_COMPILER:
CC=clang CXX=clang++ cmake
You can set these on command line when invoking cmake:
cmake -D CMAKE_C_COMPILER=clang -D CMAKE_CXX_COMPILER=clang++
You can also set these directly in your cmake file:
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
make sure however that you do that at the very top of your cmake file before any project/enable_language directive is used.