CMake does not find Visual C++ compiler

2019-01-07 05:06发布

问题:

After installing Visual Studio 2015 and running CMake on a previous project, CMake errors stating that it could not find the C compiler.

The C compiler identification is unknown
The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:4 (PROJECT):
  No CMAKE_C_COMPILER could be found.

CMake Error at CMakeLists.txt:4 (PROJECT):
  No CMAKE_CXX_COMPILER could be found.

I went searching for cl.exe in the Visual Studio folder,C:\Program Files\Microsoft Visual Studio 14.0, and could not find it.

How do I set up CMake to work on Windows with Visual Studio 2015?

回答1:

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.



回答2:

Here is the solution that worked for me:

  1. Open Visual Studio command prompt tool (as an administrator)
  2. Navigate to where you have the CMake executable
  3. Run Cmake.exe
  4. Proceed as usual to select build and source folder
  5. Select the appropriate Visual Studio compiler and hit the configure button

Hopefully it should run without problems.



回答3:

I looked in CMakeError.log file and found an error about cannot run 'rc.exe'

I searched and found this answer to copy RC.Exe and RcDll.Dll from the Microsoft SDKs bin to the VC bin, and then CMake worked.


Edit: The top answer to another question suggests that it's a PATH issue, so it could be enough to ensure the Microsoft SDK bin is in your PATH.



回答4:

Make sure you are using the correct version of Visual Studio in the generator. I had incorrectly selected Visual Studio 15 when Visual Studio 14 installed.



回答5:

Those stumbling with this on Visual Studio 2017: there is a feature related to CMake that needs to be selected and installed together with the relevant compiler toolsets. See the screenshot below.



回答6:

For me, I checked the CMakeError.log file and found:

[...] error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".

This is despite using Visual Studio 2017 on Windows 7. So it appears that CMake is trying to build its detection project with the Windows 8.1 SDK.

I used the Visual Studio installer to add that component and now CMake is happy as a clam.



回答7:

If none of the above solutions worked, then stop and do a sanity check.

I got burned using the wrong -G <config> string and it gave me this misleading error.

First, run from the VS Command Prompt not the regular command prompt. You can find it in Start Menu -> Visual Studio 2015 -> MSBuild Command Prompt for VS2015 This sets up all the correct paths to VS tools, etc.

Now see what generators are available from cmake...

cmake -help

...<snip>... The following generators are available on this platform: Visual Studio 15 [arch] = Generates Visual Studio 15 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files. Optional [arch] can be "Win64" or "IA64". ...

Then chose the appropriate string with the [arch] added.

mkdir _build cd _build cmake .. -G "Visual Studio 15 Win64"

Running cmake in a subdirectory makes it easier to do a 'clean' since you can just delete everything in that directory.

I upgraded to Visual Studio 15 but wasn't paying attention and was trying to generate for 2012.



回答8:

Menu → Visual Studio 2015 → MSBuild Command Prompt for Visual Studio 2015. Then CMake can find cl.exe.

set PATH="c:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x64\";%PATH%

Change the upper path to where your Windows SDK is installed.

CMake can find rc.exe.

cd to the path of CMakeLists.txt and do:

md .build
cd .build
cmake .. -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release
cmake --build .

The param after -G should be fetched by CMake. Use --help; you may or may not have the generator.



回答9:

As an alternative to the accepted answer, you can install C++ directly as described here.



回答10:

I ran into the same issue and fixed it by relaunching the Visual Studio Install and checking the following option:

Windows and Web Development / Universal Windows App Development Tools / Windows 10 SDK

It contains the standard C++ headers used in most applications and therefore it is often necessary to install it as well.



回答11:

In my case there was an environment variable set which was the reason for this error. The problem was solved after deleting cxx_flags from the environment variables.



回答12:

If you are on Visual Studio 2017 you need at least CMake 3.8!



回答13:

I got this problem with CMake 3.12.1, after an update of Visual Studio 2017. I simply re-ran CMake and it worked.



回答14:

In my case I could see in the CMakeError.log that CMake could not find the Windows SDK (MSB8003: Could not find WindowsSDKDir variable from the registry).

The version can be specified on the commandline on the first CMake run using:

-DCMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION=

I got further after setting that, but I hit more issues later (so I assume my environment is messed up somehow), but maybe it will help someone with this issue.



回答15:

A couple of tips:

  • Try to set the path manually by checking 'advanced' and modifying CMAKE_LINKER and CMAKE_MAKE_PROGRAM
  • Delete the cache - in the CMake with GUI go to: File → Delete Cache.

My problem was a combination of previously stated: I have set the compiler version to 15 instead of 14 and when corrected, I had to delete the cache.

I also started the Visual Studio command prompt as an administrator and from there I ran the cmake-gui.exe

Then everything worked as it was supposed to.



回答16:

In my case the issue was that the parent project, which is including googletest via

add_subdirectory(gtest_dir)

was defined as

PROJECT( projname CXX )

Somehow, CMake does not recognize

PROJECT(sub_project_name CXX C)

since the C compiler is not set in the parent.

I solved the issue by using

PROJECT( projname CXX C)

in my main CMakeLists.txt file.



回答17:

This might be another solution for those with the latest Windows 10 creator version:

Stack Overflow post Fatal error LNK1104: cannot open file 'gdi32.lib'



回答18:

None of the previous solutions worked for me. However I noticed that although I installed Visual Studio version 15 (not to be confused with Visual Studio 2015) the directory created on my computer was for Visual Studio 14.

When I specified Visual Studio 14 when I pressed the configuration button it worked.



回答19:

I had this issue under Windows 10 when using Visual Studio 2015 Professional, while Visual Studio 2015 Express worked! Under Windows 7, both Visual Studio versions used to work.

New projects created from the Visual Studio 2015 Professional IDE successfully compile, but CMake would fail to find the compiler reporting:

The C compiler identification is unknown
The CXX compiler identification is unknown

I upgraded CMake from 3.4.1 to 3.11.4, and now the problem is gone.



回答20:

I had a similar problem with the Visual Studio 2017 project generated through CMake. Some of the packages were missing while installing Visual Studio in Desktop development with C++. See snapshot:

Visual Studio 2017 Packages:

Also, upgrade CMake to the latest version.



回答21:

Because CMake is not able to find your Visual Studio compiler. Start any project which will download the required compilers and CMake must be able to find it then.