VS2017自带VS2015编译器等在命令行下无法使用问题

1、起因

早前把VS2015卸了,安装了VS2017。因为VS2017安装的时候可以选择安装VS2015编译套件,也就安装了。使用上一直没有什么问题,所以也没有注意到这个细节。
后来使用cmake生成项目工程文件的时候,选择VS2015编译器,却提示找不到C编译器。

CMake Error at CMakeLists.txt:3 (project):
  No CMAKE_CXX_COMPILER could be found.

2、解决

原本以为是环境变量没有设置好,查看了一下VS140COMNTOOLS的路径是对的。
然后试着在VS2015本机工具命令提示符工具下试试cmake行不行。结果一打开VS2015本机工具命令提示符就提示让你安装Visual Studio or C++ Build SKU

C:Program Files (x86)Microsoft Visual Studio 14.0VC>vcvarsall.bat "x86" "8.1"
Error in script usage. The correct usage is:
    vcvarsall.bat [option]
  or
    vcvarsall.bat [option] store
  or
    vcvarsall.bat [option] [version number]
  or
    vcvarsall.bat [option] store [version number]
where [option] is: x86 | amd64 | arm | x86_amd64 | x86_arm | amd64_x86 | amd64_arm
where [version number] is either the full Windows 10 SDK version number or "8.1" to use the windows 8.1 SDK
:
The store parameter sets environment variables to support
  store (rather than desktop) development.
:
For example:
    vcvarsall.bat x86_amd64
    vcvarsall.bat x86_arm store
    vcvarsall.bat x86_amd64 10.0.10240.0
    vcvarsall.bat x86_arm store 10.0.10240.0
    vcvarsall.bat x64 8.1
    vcvarsall.bat x64 store 8.1
:
Please make sure either Visual Studio or C++ Build SKU is installed.

问题看来就在这里,仔细查看了一下C:Program Files (x86)Microsoft Visual Studio 14.0VCvcvarsall.bat这个文件,发现了问题所在(见下面代码注释)。

@echo off

REM VC command prompt depends on env. variable installed during VS. This causes VC command prompt to break for C++ Build SKU.
REM So if VS is not installed and C++ Build SKU is installed, set appropriate environment for C++ Build SKU by calling into it's batch file.
REM C++ Build SKU supports only desktop development environment.

:: 上面是注释不用管,问题就出现在下面的两行
:: 下面这一行检查devenv.exe这个文件,但是这里是没有的
if exist "%~dp0..common7IDEdevenv.exe" goto setup_VS
:: 检查到没有devenv.exe,也没有wdexpress.exe文件,就跳过去检查vs build tools了
if not exist "%~dp0..common7IDEwdexpress.exe" goto setup_buildsku

:setup_VS
if "%1" == "" goto x86
if "%2" == "" goto check_platform
echo "1 is %1 2 is %2"
setlocal
set _Argument2=%2
if not "%2"=="store" if not "%2"=="8.1" if not "%_Argument2:~0,3%"=="10."  goto usage
endlocal

.... 此处省略很多行

:setup_buildsku
if not exist "%~dp0....Microsoft Visual C++ Build Toolsvcbuildtools.bat" goto usage
set CurrentDir=%CD%
call "%~dp0....Microsoft Visual C++ Build Toolsvcbuildtools.bat" %1 %2
cd /d %CurrentDir%
goto :eof

这里解决的办法很简单,把多余的两行检查注释掉就行了。(可以拷贝了一份vcvarsall.bat,在副本里面进行修改)
但是这样做只是在命令行下可以用了,cmake还是No CMAKE_CXX_COMPILER,检测不到编译器。
查看了一下CMAKE安装目录下的sharecmake-3.7ModulesCompilerMSVC-CXX.cmake文件,也没有什么收获。
但是可以通过直接指定编译器来使用,这是可以的。

cmake -DCMAKE_C_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe" -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe" .

在VS2017的安装目录下还有一份VS2015的编译环境。默认路径是C:Program Files (x86)Microsoft Visual StudioShared14.0VCin

原文地址:https://www.cnblogs.com/oloroso/p/6404475.html