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.0\VC>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.0\VC\vcvarsall.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..\common7\IDE\devenv.exe" goto setup_VS
:: 檢查到沒有devenv.exe,也沒有wdexpress.exe文件,就跳過去檢查vs build tools了
if not exist "%~dp0..\common7\IDE\wdexpress.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 Tools\vcbuildtools.bat" goto usage
set CurrentDir=%CD%
call "%~dp0..\..\Microsoft Visual C++ Build Tools\vcbuildtools.bat" %1 %2
cd /d %CurrentDir%
goto :eof
這里解決的辦法很簡單,把多余的兩行檢查注釋掉就行了。(可以拷貝了一份vcvarsall.bat,在副本里面進行修改)
但是這樣做只是在命令行下可以用了,cmake
還是No CMAKE_CXX_COMPILER
,檢測不到編譯器。
查看了一下CMAKE安裝目錄下的share\cmake-3.7\Modules\Compiler\MSVC-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 Studio\Shared\14.0\VC\bin