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.
Related
I am new in c++
The cmakelist.txt file dont give me a Makefile
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22000.
-- The C compiler identification is MSVC 19.29.30140.0
-- The CXX compiler identification is MSVC 19.29.30140.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/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/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/JohnSmith/Documents/Hello/build
I dont know how to change the compiler to G++
this is the cmake
cmake_minimum_required(VERSION 3.0)
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
project(HelLo)
add_executable(Hello main.cpp hello.cpp)
I am on windows , Thanks for help
(I can compile with g++ and gcc just cmakefile dont work )
First, make sure you've installed gcc/g++ under Windows (either CygWin or MinGW). Remove the set commands from your CMakelists.txt. Then in a CygWin/MinGW shell use the following command to generate the Makefile:
CC=gcc CXX=c++ cmake ..
cmake -G "Unix Makefiles" ..
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".
I am trying to build and install a basic program with CMake 3.17.2 for 64 bit windows with Visual Studio 16 2019.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.13.5)
project(Test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "/EHsc")
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
message("IS DEFAULT: ${CMAKE_INSTALL_PREFIX}")
else()
message("NOT DEFAULT: ${CMAKE_INSTALL_PREFIX}")
endif()
message("PLATFORM = ${CMAKE_VS_PLATFORM_NAME_DEFAULT}")
add_executable(main main.cpp)
install(TARGETS main DESTINATION bin)
However, when running the following commands:
$ mkdir build
$ cd build
$ cmake ..
I get the following output:
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.27.29111.0
-- The CXX compiler identification is MSVC 19.27.29111.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/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/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/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
IS DEFAULT: C:/Program Files (x86)/Test
PLATFORM = x64
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/.../cmake-test/build
Why is the CMAKE_INSTALL_PREFIX defaulting to Program Files x86?
The CMake docs for CMAKE_INSTALL_PREFIX suggest it should be otherwise:
This variable defaults to /usr/local on UNIX and c:/Program Files/${PROJECT_NAME} on Windows.
Building confirms that the exe is really 64 bits:
$ cmake --build .
$ file Debug/main.exe
Debug/main.exe: PE32+ executable (console) x86-64, for MS Windows
Confirming that CMake itself is 64 bits, and explicitly running with that, gives the same output:
$ file /c/Program\ Files/CMake/bin/cmake.exe
/c/Program Files/CMake/bin/cmake.exe: PE32+ executable (console) x86-64, for MS Windows
# in CMD to remove git bash from the equation...
$ echo %PROGRAMFILES%
C:\Program Files
$ "C:\Program Files\CMake\bin\cmake.exe" ..
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
...
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
...
IS DEFAULT: C:/Program Files (x86)/Test
PLATFORM = x64
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/.../cmake-test/build
I believe this was happening because, while CMake was assuming VS 2019 as the generator, it was not assuming x64 as the architecture.
The architecture can be set with the -A flag:
cmake -G "Visual Studio 17 2022" -A x64
Explicitly setting the generator may or may not be necessary. Regardless, that seems to do the trick for the install.
I am following the instructions from https://badprog.com/c-boost-setting-up-on-windows-10 . I have Visual Studio 2017 installed, and installed boost_1_71_0-msvc-14.1-64.exe. CMake was downloaded from cmake.org/download/, cmake-3.19.0-rc1-win64-x64.msi.
I have a project that needs Boost Beast, which I believe is 1.70 or higher.
When I run cmake . everything seems fine:
C:\Users\Public\xyz>cmake .
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
-- 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/BuildTools/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
-- Found Threads: TRUE
-- Found Boost: C:/local/boost_1_71_0 (found suitable version "1.71.0", minimum required is "1.70") found components: thread system chrono date_time atomic
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Public/xyz
The resulting project opens in Visual Studio 2017, but then complains it needs the 2019 build tools. Surprisingly, as you can see above, it's building for Visual Studio 2019?
But, after killing CMakeCache.txt and CMakeFiles, if I do this:
C:\Users\Public\xyz>cmake -G "Visual Studio 15 2017" .
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
-- The CXX compiler identification is MSVC 19.16.27034.0
-- 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/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
-- Found Threads: TRUE
CMake Error at C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
Could NOT find Boost (missing: thread system) (found suitable version
"1.71.0", minimum required is "1.70")
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:577 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.19/Modules/FindBoost.cmake:2176 (find_package_handle_standard_args)
CMakeLists.txt:29 (FIND_PACKAGE)
-- Configuring incomplete, errors occurred!
See also "C:/Users/Public/xyz/CMakeFiles/CMakeOutput.log".
Suddenly it can't find Boost components (but can find Boost?). I've tried various forms of -DBOOST_LIBRARYDIR or -DBOOST_ROOT, etc., to no avail, but I don't seem to have the usual stackoverflow problem of cmake not being able to find Boost; instead it doesn't seem to recognize the Visual Studio 2017 target, despite having been installed for msvc-14.1?
CMakeLists.txt:
# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.1)
# Force C++11, for Boost Beast (async http library)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Project's name
project(xyz
VERSION 0.1
LANGUAGES CXX)
# Set the output folder where the program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
# Specify .cpp and .hpp is in src/
set(PROJECT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)
include_directories("${PROJECT_SOURCE_DIR}")
# Add source to build target for hello world...
add_executable(main ${PROJECT_SOURCE_DIR}/main.cpp)
# Testing http support
ADD_EXECUTABLE( server ${PROJECT_SOURCE_DIR}/server.cpp )
FIND_PACKAGE( Boost 1.70 REQUIRED COMPONENTS thread system )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} )
target_link_libraries ( server LINK_PUBLIC ${Boost_LIBRARIES} )
In the end, the issue was simple. If I may so, it was an easy mistake for a beginner to make but almost impossible for a beginner to unravel with regular google searching.
When I issued cmake ., it was defaulting to a 64 bit build, but assuming a Visual Studio 2019 target. That was wrong.
The equivalent should have been cmake -G "Visual Studio 15 2017 Win64" . But not knowing this, and following the wrong tutorials, I was issuing instead cmake -G "Visual Studio 15 2017" .. This defaulted to a 32 bit build - though by default it never mentions this fact. It could find boost, but all library files were for 64 bit and useless for 32 bit, so it simply said "I can't find anything!".
My thanks to #Tsyvarev, who should get the credit for the answer. Turning on DEBUG and looking at searched library files character by character was indeed the right call.
I'm currently getting this error while trying to configure a project on CMAKE using Visual Studio 15 2017 Win64:
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: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x64/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x64/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x64/cl.exe
Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.11.25503/bin/Hostx86/x64/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
Found OpenGL: opengl32
Looking for pthread.h
Looking for pthread.h - not found
Found Threads: TRUE
Using Win32 for window creation
Using WGL for context creation
Lib glbinding
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
I've used this compiler to generate another project and it still works.
Here is the code relevant to the new CMakeLists.txt:
project(ms3d_td3)
cmake_minimum_required(VERSION 3.2.0)
add_subdirectory(ext/glfw)
add_subdirectory(ext/glbinding)
include_directories(ext/glfw/include)
include_directories(ext/glbinding/include)
include_directories(ext/eigen3)
if(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
set(SRC_FILES
src/main.cpp
src/viewer.cpp
src/viewer.h
src/shader.cpp
src/shader.h
src/opengl.h)
add_definitions(-DDATA_DIR="${PROJECT_SOURCE_DIR}/data")
add_executable(ms3d_td3 ${SRC_FILES})
target_link_libraries(ms3d_td3 glfw ${GLFW_LIBRARIES} glbinding)
Under Visual Studio 2017 I get a CMAKE_C_COMPILER not set after EnableLanguage. I tried to set it manually to the VisualStudio 2017 compiler but no success. Any ideas?
Another person had a similar problem and solved it by upgrading Visual Studio a while ago so I'm guessing this is coming from somewhere else... :
Compiler failing on C++11 instructions in a Visual Studio project configured with cmake
Thanks!
To require compiler support for the [[decprecated]] attribute, use target_compile_features and specify the cxx_attribute_deprecated feature. For example:
add_executable(foo foo.cpp)
target_compile_features(foo PRIVATE cxx_attribute_deprecated)
If you need to conditionally test for this so that you can configure a header file, use check_cxx_source_compiles.