问题描述:
有两位同事先后使用 VS2017 下载包安装到新电脑,安装盘选的非系统盘。安装完后,均出现普通工程可正常编译运行,CMake 工程无法编译的错误,其中第一位同事报错:
1> -- The C compiler identification is MSVC 19.16.27042.0
1> -- The CXX compiler identification is MSVC 19.16.27042.0
1> -- Check for working C compiler: E:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe
1> -- Check for working C compiler: E:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe -- broken
1> -- Configuring incomplete, errors occurred!
...
1> CMake Error at E:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:52 (message):
1> The C compiler
1>
1> "E:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe"
1>
1> is not able to compile a simple test program.
1>
...
1>
1> Run Build Command:"E:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe" "cmTC_4ed4b"
1> [1/2] Building C object CMakeFiles\cmTC_4ed4b.dir\testCCompiler.c.obj
1> [2/2] Linking C executable cmTC_4ed4b.exe
1> FAILED: cmTC_4ed4b.exe
1> cmd.exe /C "cd . && "E:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_4ed4b.dir --manifests -- "E:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\link.exe" /nologo CMakeFiles\cmTC_4ed4b.dir\testCCompiler.c.obj /out:cmTC_4ed4b.exe /implib:cmTC_4ed4b.lib /pdb:cmTC_4ed4b.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
1> RC Pass 1: command "rc /foCMakeFiles\cmTC_4ed4b.dir/manifest.res CMakeFiles\cmTC_4ed4b.dir/manifest.rc" failed (exit code 0) with the following output:
1> 系统找不到指定的文件。
1> ninja: build stopped: subcommand failed.
1>
1>
1>
1>
1> CMake will not be able to correctly generate this project.
1> Call Stack (most recent call first):
1> D:\CMakeProject1\CMakeLists.txt(6): (project)
重点在于倒数第7~4行:
RC Pass 1: command "rc /foCMakeFiles\cmTC_4ed4b.dir/manifest.res CMakeFiles\cmTC_4ed4b.dir/manifest.rc" failed (exit code 0) with the following output:
1> 系统找不到指定的文件。
第二位同事报错:
1> -- The C compiler identification is MSVC 19.16.27042.0
1> -- The CXX compiler identification is MSVC 19.16.27042.0
1> -- Check for working C compiler: D:/sofeware/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe
1> -- Check for working C compiler: D:/sofeware/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe -- broken
1> The C compiler
1>
1> "D:/sofeware/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/cl.exe"
1>
1> is not able to compile a simple test program.
1>
...
1>
1> Run Build Command:"D:\SOFEWARE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "cmTC_fb744"
1> [1/2] Building C object CMakeFiles\cmTC_fb744.dir\testCCompiler.c.obj
1> [2/2] Linking C executable cmTC_fb744.exe
1> FAILED: cmTC_fb744.exe
1> cmd.exe /C "cd . && D:\sofeware\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_fb744.dir --manifests -- D:\sofeware\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_fb744.dir\testCCompiler.c.obj /out:cmTC_fb744.exe /implib:cmTC_fb744.lib /pdb:cmTC_fb744.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
1> LINK Pass 1: command "D:\sofeware\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_fb744.dir\testCCompiler.c.obj /out:cmTC_fb744.exe /implib:cmTC_fb744.lib /pdb:cmTC_fb744.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_fb744.dir/intermediate.manifest CMakeFiles\cmTC_fb744.dir/manifest.res" failed (exit code 1104) with the following output:
1> LINK : fatal error LNK1104: 无法打开文件“kernel32.lib”
1> ninja: build stopped: subcommand failed.
1>
1>
1>
1>
1> CMake will not be able to correctly generate this project.
LINK : fatal error LNK1104: 无法打开文件“kernel32.lib”
第二位同事报错信息更加明确,是找不到“kernel32.lib”,也就是需要引用的系统库路径等没有配置。
解决方法:
添加了如下环境变量:
其中%KITS_HOME%为 Windows Kits 的路径,图中定义为了 D:\Windows Kits\10
最后一个 %VS2017_HOME%\Common7\IDE 可能不需要。
问题原因推测:
可能是安装的时候,CMake 没有自动配置相关的头文件路径和库路径;不知道是不是没有采用默认安装到系统盘的原因,或者使用 iso 安装的话是否也会出现这样的问题。
非 CMake 的 VS 工程可正常编译运行,是因为其工程配置中有默认的头文件路径和库路径变量.
之前自己也是用相同的下载包安装的 VS2017 ,没有额外手动配置,安装完就能正常使用 CMake,但是在那之前通过 iso 安装过 VS2012,感觉应该没关系。。不好猜测具体是什么原因。