Cmake Does Not Find Visual C++ Compiler

CMake does not find Visual C++ compiler

I have found the solution. While the Visual Studio IDE installed successfully it did not install any build tools and therefore did not install the C++ compiler.

By attempting to manually create a C++ project in the Visual Studio 2015 GUI I was able to prompt it to download the C++ packages. CMake was then able to find the compiler without any difficulty.

CMake does not find VS 2017 C++ compiler after installation of VS 2019

CMake always tries to find the most recent version of Visual Studio. Therefore older releases of CMake before 3.14 may fail if VS2019 is installed and not properly detected.

Please note that you need to delete CMakeCache.txt and CMakeFiles folder from your build directory if rerunning.

If you intend to use VS2019 with CMake you need to use the architecture option (-A) of CMake as there is no "Visual Studio 16 2019 Win64" generator, e.g. cmake -G "Visual Studio 16 2019" -A x64 for a 64-bit build or cmake -G "Visual Studio 16 2019" -A Win32 for a 32-bit build. The architecture option was introduced in CMake 3.0.2, so you can use that instead of the specific generator name for older Visual Studio installations too.

Visual Studio 2019 with C++ has been installed but CMake can't find it

I had the same problem. It was probably related to the plugins and extensions you have installed. My issue was not resolved until I reinstalled Visual Studio. Try to install it again.

CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found

Those error messages

CMake Error at ... (project):
No CMAKE_C_COMPILER could be found.
-- Configuring incomplete, errors occurred!
See also ".../CMakeFiles/CMakeOutput.log".
See also ".../CMakeFiles/CMakeError.log".

or

CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.
Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
...
-- Configuring incomplete, errors occurred!

just mean that CMake was unable to find your C/CXX compiler to compile a simple test program (one of the first things CMake tries while detecting your build environment).

The steps to find your problem are dependent on the build environment you want to generate. The following tutorials are a collection of answers here on Stack Overflow and some of my own experiences with CMake on Microsoft Windows 7/8/10 and Ubuntu 14.04.

Preconditions

  • You have installed the compiler/IDE and it was able to once compile any other program (directly without CMake)

    • You e.g. may have the IDE, but may not have installed the compiler or supporting framework itself like described in Problems generating solution for VS 2017 with CMake or How do I tell CMake to use Clang on Windows?
  • You have the latest CMake version

  • You have access rights on the drive you want CMake to generate your build environment

  • You have a clean build directory (because CMake does cache things from the last try) e.g. as sub-directory of your source tree

    Windows cmd.exe

      > rmdir /s /q VS2015
    > mkdir VS2015
    > cd VS2015

    Bash shell

      $ rm -rf MSYS
    $ mkdir MSYS
    $ cd MSYS

    and make sure your command shell points to your newly created binary output directory.

General things you can/should try

  1. Is CMake able find and run with any/your default compiler? Run without giving a generator

     > cmake ..
    -- Building for: Visual Studio 14 2015
    ...

    Perfect if it correctly determined the generator to use - like here Visual Studio 14 2015

  2. What was it that actually failed?

    In the previous build output directory look at CMakeFiles\CMakeError.log for any error message that make sense to you or try to open/compile the test project generated at CMakeFiles\[Version]\CompilerIdC|CompilerIdCXX directly from the command line (as found in the error log).

CMake can't find Visual Studio

  1. Try to select the correct generator version:

     > cmake --help
    > cmake -G "Visual Studio 14 2015" ..
  2. If that doesn't help, try to set the Visual Studio environment variables first (the path could vary):

     > "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
    > cmake ..

    or use the Developer Command Prompt for VS2015 short-cut in your Windows Start Menu under All Programs/Visual Studio 2015/Visual Studio Tools (thanks at @Antwane for the hint).

Background: CMake does support all Visual Studio releases and flavors (Express, Community, Professional, Premium, Test, Team, Enterprise, Ultimate, etc.). To determine the location of the compiler it uses a combination of searching the registry (e.g. at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\[Version];InstallDir), system environment variables and - if none of the others did come up with something - plainly try to call the compiler.

CMake can't find GCC (MinGW/MSys)

  1. You start the MSys bash shell with msys.bat and just try to directly call gcc

     $ gcc
    gcc.exe: fatal error: no input files
    compilation terminated.

    Here it did find gcc and is complaining that I didn't gave it any parameters to work with.

    So the following should work:

     $ cmake -G "MSYS Makefiles" ..
    -- The CXX compiler identification is GNU 4.8.1
    ...
    $ make

    If GCC was not found call export PATH=... to add your compilers path (see How to set PATH environment variable in CMake script?) and try again.

  2. If it's still not working, try to set the CXX compiler path directly by exporting it (path may vary)

     $ export CC=/c/MinGW/bin/gcc.exe
    $ export CXX=/c/MinGW/bin/g++.exe
    $ cmake -G "MinGW Makefiles" ..
    -- The CXX compiler identification is GNU 4.8.1
    ...
    $ mingw32-make

    For more details see How to specify new GCC path for CMake

    Note: When using the "MinGW Makefiles" generator you have to use the mingw32-make program distributed with MinGW

  3. Still not working? That's weird. Please make sure that the compiler is there and it has executable rights (see also preconditions chapter above).

    Otherwise the last resort of CMake is to not try any compiler search itself and set CMake's internal variables directly by

     $ cmake -DCMAKE_C_COMPILER=/c/MinGW/bin/gcc.exe -DCMAKE_CXX_COMPILER=/c/MinGW/bin/g++.exe ..

    For more details see Cmake doesn't honour -D CMAKE_CXX_COMPILER=g++ and Cmake error setting compiler

    Alternatively those variables can also be set via cmake-gui.exe on Windows. See Cmake cannot find compiler

Background: Much the same as with Visual Studio. CMake supports all sorts of GCC flavors. It searches the environment variables (CC, CXX, etc.) or simply tries to call the compiler. In addition it will detect any prefixes (when cross-compiling) and tries to add it to all binutils of the GNU compiler toolchain (ar, ranlib, strip, ld, nm, objdump, and objcopy).

CMake on Windows

Because CMake's error message is misleading here, I think it warrants a little more detailed answer.

In short, you ran into a chicken-and-egg kind of a problem.

CMake's compiler detection is mighty, but since - during the first try -

  • you didn't give any explicit generator to use with -G
  • it couldn't find a Visual Studio installed
  • it couldn't find any C/C++ compiler in your PATH environment
  • it couldn't find a CC environment variable defined with the full path to a compiler

It was defaulting to nmake.

Now here comes the problem: it does remember your implicit generator/compiler choice in it's variable cache (see CMAKE_GENERATOR in CMakeCache.txt). What is a very useful feature, if you have multiple compilers installed.

But if you then declare the CC environment variable - as the error message suggests - it's too late since your generator's choice was remembered in the first try.

I see two possible ways out of this:

  1. Overrule the generator choice by given the right one with cmake.exe -G "MinGW Makefiles" .. (as the answer linked by @Guillaume suggests)
  2. Delete your project's binary output directory (including CMakeCache.txt) and do cmake.exe .. after you added your compiler's bin folder to your PATH environment.

References

  • Running CMake on Windows
  • What is the default generator for CMake in Windows?
  • CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
  • CMake: how to specify the version of Visual C++ to work with?


Related Topics



Leave a reply



Submit