conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.26k stars 980 forks source link

[bug] CMake Error at CMakeLists.txt:3 (project): No CMAKE_CXX_COMPILER could be found. #14232

Open aiyolo opened 1 year ago

aiyolo commented 1 year ago

Environment details

Steps to reproduce

  1. run conan profile detect --force to generate a default profile
  2. add a line *:tools.cmake.cmaketoolchain:generator=Ninja in profile to use Ninja as generator
  3. run conan install . -of build -b missing -s build_type=Debug
  4. run cmake --preset conan-debug, then errors comes up with:

    
    -- Conan toolchain: C++ Standard 14 with extensions OFF
    -- The CXX compiler identification is unknown
    CMake Error at CMakeLists.txt:3 (project):
    No CMAKE_CXX_COMPILER could be found.
    
    Tell CMake where to find the compiler by setting either the environment
    variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
    to the compiler, or to the compiler name if it is in the PATH.

-- Configuring incomplete, errors occurred!


### Logs

profile

[settings] arch=x86_64 build_type=Release compiler=msvc compiler.cppstd=14 compiler.runtime=dynamic compiler.version=193 os=Windows

[conf] *:tools.cmake.cmaketoolchain:generator=Ninja


conanfile.txt

[requires] boost/1.78.0

[generators] CMakeDeps CMakeToolchain


CMakelists.txt

cmake_minimum_required(VERSION 3.17)

project(network-assist LANGUAGES CXX) ...

aiyolo commented 1 year ago

everything is well when I don't specify Ninja as generator(then Visual Studio 16 will be choosed as default generator), but if I specify Ninja as generator, the errors comes up.

memsharded commented 1 year ago

Hi @aiyolo

Thanks for your question. This happens because the Ninja generator does not locate the VS installation by itself (this is something between CMake, Ninja and VS, not specific of Conan).

So it is necessary to activate the VS prompt, of the compiler you want to use. In the conan install process Conan generates automatically a conanbuild.bat script that activate all the necessary environment (not only the vcvars, for example it could also activate the PATH to use a modern cmake version that was required by Conan with tool_requires("cmake/[>=3.25]")).

This works:

$ build\Debug\generators\conanbuild.bat
conanvcvars.bat: Activating environment Visual Studio 17 - amd64 - vcvars_ver=14.3
[vcvarsall.bat] Environment initialized for: 'x64'

$ cmake --preset conan-debug
Preset CMake variables:

  CMAKE_BUILD_TYPE="Debug"
  CMAKE_POLICY_DEFAULT_CMP0091="NEW"
  CMAKE_TOOLCHAIN_FILE:FILEPATH="C:\----\build\Debug\generators\conan_toolchain.cmake"

-- Using Conan toolchain: C:/---/build/Debug/generators/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
-- The CXX compiler identification is MSVC 19.36.32534.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/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: 
aiyolo commented 1 year ago

There still remains a problem, Since I installed qt mingw g++ in my computer, after run conanbuild.bat, and cmake --preset conan-debug, mingw g++ is detected as default compiler instead of visual studio, why this happens?

$ \build\conanbuild.bat
conanvcvars.bat: Activating environment Visual Studio 17 - amd64 - vcvars_ver=14.3
[vcvarsall.bat] Environment initialized for: 'x64'

$ cmake --preset conan-debug
Preset CMake variables:

  CMAKE_BUILD_TYPE="Debug"
  CMAKE_POLICY_DEFAULT_CMP0091="NEW"
  CMAKE_TOOLCHAIN_FILE:FILEPATH="C:\Users\zhu\Documents\qiaoyi\cmake-qt-template\conan-template\build\conan_toolchain.cmake"

-- Using Conan toolchain: C:/Users/zhu/Documents/qiaoyi/cmake-qt-template/conan-template/build/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- The CXX compiler identification is GNU 7.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Qt/Qt5.13.2/Tools/mingw730_64/bin/c++.exe - skipped
memsharded commented 1 year ago

That might be an extra side effect of using Ninja generator, that can do its own thing if things are not defined how it expects it to be, and as it is not a Microsoft thing, it might prioritize other compilers if they are found with higher priority, and it has not been explicitly told otherwise. Maybe the CC/CXX env-vars are defined, or just that qt compiler is prioritized in the PATH (by the way, it that is not your intended default compiler, I'd recommend to remove it from the PATH env-var).

I don't know how that mingw qt works, but maybe the solution is to explicitly define something like this in the profile:

[conf]
tools.build:compiler_executables={"c": "cl", "cpp": "cl"}

In any case, I'd recommend:

aiyolo commented 1 year ago

I just ran env to recheck my PATH, but I didn't find CC/CXX in PATH, and there is no qt mingw g++

then I set tools.build:compiler_executables={"c": "cl", "cpp": "cl"} in my profile, and rut it again, but it seems I need to provide the full path of my msvc compiler.

$ cmake --preset conan-debug
Preset CMake variables:

  CMAKE_BUILD_TYPE="Debug"
  CMAKE_POLICY_DEFAULT_CMP0091="NEW"
  CMAKE_TOOLCHAIN_FILE:FILEPATH="C:\Users\zhu\Documents\qiaoyi\cmake-qt-template\conan-template\build\conan_toolchain.cmake"

-- Using Conan toolchain: C:/Users/zhu/Documents/qiaoyi/cmake-qt-template/conan-template/build/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
  The CMAKE_CXX_COMPILER:

    cl

  is not a full path and was not found in the PATH.
memsharded commented 1 year ago

CC and CXX could be environment variables, different from PATH. I guess you checked that they are not defined either?

How would be cmake aware of such a c++ compiler in C:/Qt/Qt5.13.2/Tools/mingw730_64/bin/c++.exe if it is not in the PATH? The information should be somewhere, maybe in the build scripts, in the environment, etc. In Windows, CMake will not find arbitrary compilers in any random location unless the information is somewhere.

aiyolo commented 1 year ago

Now I have removed all compilers except for msvc, and continued with the previous approach, but it still doesn't work. the error is:

$ cmake --preset conan-release
Preset CMake variables:

  CMAKE_BUILD_TYPE="Release"
  CMAKE_POLICY_DEFAULT_CMP0091="NEW"
  CMAKE_TOOLCHAIN_FILE:FILEPATH="C:\---\cmake-qt-template\conan-template\build\conan_toolchain.cmake"

-- Using Conan toolchain: C:/---/cmake-qt-template/conan-template/build/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- The CXX compiler identification is MSVC 19.36.32535.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/---/scoop/apps/cmake/3.26.4/share/cmake-3.26/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/zhu/Documents/qiaoyi/cmake-qt-template/conan-template/build/CMakeFiles/CMakeScratch/TryCompile-q7znqu
memsharded commented 1 year ago

I don't know what could be happening, some things to check:

sully7 commented 1 year ago

FYI - this seemed to fix the issue for me! In my case, I needed to set version 10.22000.0 once, using cmake -DCMAKE_SYSTEM_VERSION=10.0.22000.0 - then it seemed to build fine from there.

Note - I had a cleanly installed version of VS 2022

memsharded commented 1 year ago

Ok, that is interesting information thanks @sully7

Conan has a mechanism to explicitly define the cmake-system-version when it is necessary (this doesn't seem a Conan issue, but a CMake-VS version-Windows SDK version one): tools.cmake.cmaketoolchain:system_version conf can be defined in profile or command line, and it will set the CMAKE_SYSTEM_VERSION in the toolchain

Could you @aiyolo, @sully7, please try it?

aiyolo commented 1 year ago

Here is what I try. for default profile

[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=193
os=Windows

[conf]
tools.cmake.cmaketoolchain:system_version=10.0.19041.0
tools.cmake.cmaketoolchain:generator=Ninja

After run conan install ... and activate environment, it still has some problem.

$ cmake --preset conan-debug
Preset CMake variables:

  CMAKE_BUILD_TYPE="Debug"
  CMAKE_POLICY_DEFAULT_CMP0091="NEW"
  CMAKE_TOOLCHAIN_FILE:FILEPATH="C:\Users\zhu\Documents\qiaoyi\cmake-qt-template\conan-template\build\conan_toolchain.cmake"

-- Using Conan toolchain: C:/Users/zhu/Documents/qiaoyi/cmake-qt-template/conan-template/build/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- The CXX compiler identification is MSVC 19.29.30148.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- 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
-- 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 - broken
CMake Error at C:/Users/zhu/scoop/apps/cmake/3.26.4/share/cmake-3.26/Modules/CMakeTestCXXCompiler.cmake:60 (message):
  The C++ compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.
aiyolo commented 1 year ago

And without specifying ninja generator in my default profile, my project can be built successfully.

$ cmake --preset conan-default
Preset CMake variables:

  CMAKE_POLICY_DEFAULT_CMP0091="NEW"
  CMAKE_TOOLCHAIN_FILE:FILEPATH="C:\Users\zhu\Documents\qiaoyi\cmake-qt-template\conan-template\build\conan_toolchain.cmake"

-- Using Conan toolchain: C:/Users/zhu/Documents/qiaoyi/cmake-qt-template/conan-template/build/conan_toolchain.cmake
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- The CXX compiler identification is MSVC 19.36.32535.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Conan: Component target declared 'Boost::diagnostic_definitions'
-- Conan: Component target declared 'Boost::disable_autolinking'
-- Conan: Component target declared 'Boost::dynamic_linking'
aiyolo commented 1 year ago

I don't know why, but running cmake commands in devepoler powershell might solve this question.

aiyolo commented 1 year ago

A a little bit more process about this issue. I fount that to activate windows build environment, we must activate it in the cmd shell, and the powershell will fail to activate it, for more informoation see this link