无法使用Visual Studio 2015命令行工具编译和链接简单的C ++程序 [英] Unable to compile and link simple C++ program with Visual Studio 2015 command line tools
问题描述
使用Visual Studio 2015我无法再使用命令行工具编译和链接一个简单的C ++程序。
考虑main.cpp:
#include< stdlib.h>
目录会混淆尝试确定可用的最新SDK版本的批处理文件。例如,而不是
int main(){return 0; }在以前的版本中(例如Visual Studio 2012),我能够编译和链接main.cpp容易:C:\Users\bkircher\src\test> cl main.cpp
Microsoft (R)C / C ++优化编译器版本17.00.61030 for x64
版权所有(C)Microsoft Corporation。版权所有。
main.cpp
Microsoft(R)增量链接器版本11.00.61030.0
版权所有(C)Microsoft Corporation。版权所有。
/out:main.exe
main.obj
完成。
但是,在Visual Studio 2015中,我不再有适当的CRT包含和库路径设置:
C:\Users\bkircher\src\test> cl main.cpp
Microsoft(R)C / C ++优化编译器版本19.00.23026 for x64
版权所有(C)Microsoft Corporation。版权所有。
main.cpp
main.cpp(1):致命错误C1083:无法打开包含文件:'stdlib.h':没有这样的文件或目录
我理解Microsoft将C运行时作为一个新的Windows操作系统组件,即Universal CRT。
如介绍通用CRT ,我应该使用以下MSBuild属性来找到适当的路径
$(UniversalCRT_IncludePath)
$ (UniversalCRT_LibraryPath_x64)
尽管如此,如何获得正确的库和包含除devenv之外的构建系统的路径或MSBuild?
为此:
C: \Program Files(x86)\Microsoft Visual Studio 14.0 \VC> set include
INCLUDE = C:\Program Files(x86)\Microsoft Visual Studio 14.0 \VC\INCLUDE; C: \\ Program Files文件(x86)\Microsoft Visual Studio 14.0 \VC\ATLMFC\INCLUDE; C:\Program文件(x86)\Windows Kits\10\include\wdf\ucrt; C: \Program Files(x86)\Windows Kits\NETFXSDK\4.6\\\\\\\\\\\\\\\\\\\\\\\\\\\\\; C:\Program Files(x86)\Windows Kits \10 \include\wdf \shared; C:\ Program Files(x86)\Windows Kits \10\include\wdf\um; C:\Program Files(x86)\Windows Kits\10\include\wdf\ winrt;
C:\Program Files(x86)\ Microsoft Visual Studio 14.0 \VC> set lib
LIB = C:\Program Files(x86)\Microsoft Visual Studio 14.0 \VC\LIB\amd64; C:\Program文件(x86)\Microsoft Visual Studio 14.0 \VC\ATLMFC\LIB\amd64; C:\Program文件(x86)\Windows Kits \10 \lib \wdf \ucrt\x64; C:\Program Files(x86)\Windows Kits\NETFXSDK\4.6\lib\um\x64; C:\程序文件(x86)\Windows Kits\10\lib\wdf\um\x64;
LIBPATH = C:\Windows \Microsoft.NET\Framework64\v4.0.30319; C:\Program文件(x86)\Microsoft Visual Studio 14.0 \VC\LIB\amd64; C:\ Program Files(x86)\Microsoft Visual Studio 14.0 \VC\ATLMFC\LIB\amd64; C:\Program Files(x86)\Windows Kits\10 \UnionMetadata; \ Program Files(x86)\Windows Kits \10\References; C:\Program Files(x86)\Windows Kits\10\References \Windows.Foundation.UniversalApiContract\ 1.0.0.0; C:\程序文件(x86)\Windows Kits \10\References \Windows.Foundation.FoundationContract\.0.0.0; C:\Program Files(x86)\Windows Kits参考文献\ Windows.Networking.Connectivity.WwanContract\ 1.0.0.0; C:\Program文件(x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\Microsoft.VCLibs\14.0\References \CommonConfiguration\\\
eutral;
解决方案包含环境变量的内容是一个好主意。根据出现的路径,似乎你有Windows驱动程序工具包安装,你遇到这
根据问题的描述,<$由WDK创建的c $ c> wdf
C:\Program Files(x86)\Windows Kits \10\include\变量
INCLUDE
中的wdf \ucrt
,你应该有像
C:\Program Files(x86)\Windows Kits \10\include \\10.0.10150.0\ucrt
-bomb解决方案:卸载WDK,确保
wdf
目录已经消失,并且应该恢复正常。
< hr>
如果这不是一个选项,这里是一个外科解决方案:您需要编辑
C:\Program Files(x86)\Microsoft Visual Studio 14.0 \Common7\Tools\vcvarsqueryregistry.bat
(当然是先备份)
1。 :
:GetWindowsSdkDirHelper32
:GetWindowsSdkDirHelper64
在每个文件下,您会看到以下行:
@ REM获取Windows 10 sdk版本号
@if不是%WindowsSdkDir%==@FOR / Fdelims =%% i IN('dir%WindowsSdkDir%include\/ b / ad -h / on')DO @set WindowsSDKVersion = %% i \
更改为:
@REM获取Windows 10 sdk版本号
@if不是%WindowsSdkDir%== Fdelims =%% i IN('dir%WindowsSdkDir%include\/ b / ad-h / on')DO(
@if not%% i==wdf
@set WindowsSDKVersion = %% i \
)
)
2。查找以下两个标签:
:GetUniversalCRTSdkDirHelper32
:GetUniversalCRTSdkDirHelper64
在每个项下,更改以下行:
@FOR / Fdelims =%% i IN('dir%UniversalCRTSdkDir%include\/ b / ad-h / on')DO @SET UCR- = %% i
到:
@FOR / Fdelims =%% i IN('dir%UniversalCRTSdkDir%include\/ b / ad-h / on')DO(
@if不是%% i==wdf(
@SET UCRTVersion = %% i
)
)
就是这样。让我知道是否有帮助。
请记住,这将跳过
wdf
目录。如果WDK命令提示设置脚本碰巧使用相同的vcvarsqueryregistry.bat
批处理文件(我怀疑,但是...),那么它们将不再正常工作;在这种情况下需要更多的黑客来为每个构建环境选择适当的批处理文件。With Visual Studio 2015 I am no longer able to compile and link a simple C++ program using the command line tools.
Consider main.cpp:
#include <stdlib.h> int main() { return 0; }
In previous releases (for example Visual Studio 2012) I was able to compile and link main.cpp easily:
C:\Users\bkircher\src\test>cl main.cpp Microsoft (R) C/C++ Optimizing Compiler Version 17.00.61030 for x64 Copyright (C) Microsoft Corporation. All rights reserved. main.cpp Microsoft (R) Incremental Linker Version 11.00.61030.0 Copyright (C) Microsoft Corporation. All rights reserved. /out:main.exe main.obj
And done.
With Visual Studio 2015 however, I no longer have proper CRT include and library paths set:
C:\Users\bkircher\src\test>cl main.cpp Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x64 Copyright (C) Microsoft Corporation. All rights reserved. main.cpp main.cpp(1): fatal error C1083: Cannot open include file: 'stdlib.h': No such file or directory
I understand that Microsoft distributes the C runtime as a new Windows operating system component, the Universal CRT.
As described in Introducing the Universal CRT, I should use following MSBuild properties to find the appropriate paths
$(UniversalCRT_IncludePath) $(UniversalCRT_LibraryPath_x64)
Despite that, how do I get proper library and include paths for build systems other than devenv or MSBuild?
For the sake of it:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC>set include INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE;C:\Program Files (x86)\Windows Kits\10\include\wdf\ucrt;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\include\um;C:\Program Files (x86)\Windows Kits\10\include\wdf\shared;C:\Program Files (x86)\Windows Kits\10\include\wdf\um;C:\Program Files (x86)\Windows Kits\10\include\wdf\winrt; C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC>set lib LIB=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB\amd64;C:\Program Files (x86)\Windows Kits\10\lib\wdf\ucrt\x64;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.6\lib\um\x64;C:\Program Files (x86)\Windows Kits\10\lib\wdf\um\x64; LIBPATH=C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64;C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB\amd64;C:\Program Files (x86)\Windows Kits\10\UnionMetadata;C:\Program Files (x86)\Windows Kits\10\References;C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.UniversalApiContract\1.0.0.0;C:\Program Files (x86)\Windows Kits\10\References\Windows.Foundation.FoundationContract\1.0.0.0;C:\Program Files (x86)\Windows Kits\10\References\indows.Networking.Connectivity.WwanContract\1.0.0.0;C:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\Microsoft.VCLibs\14.0\References\CommonConfiguration\neutral;
解决方案Including the contents of the environment variables was a good idea. Based on the paths appearing there, it seems that you have the Windows Driver Kit installed and you're encountering this issue reported on Connect.
According to the description of the issue, the
wdf
directory created by the WDK confuses the batch file that tries to determine the latest SDK versions available. For example, instead ofC:\Program Files (x86)\Windows Kits\10\include\wdf\ucrt
in the
INCLUDE
variable, you should have something likeC:\Program Files (x86)\Windows Kits\10\include\10.0.10150.0\ucrt
The "carpet-bombing" solution: uninstall the WDK, make sure the
wdf
directories are gone, and things should return to normal.
If that's not an option, here's a "surgical" solution: you need to edit
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\vcvarsqueryregistry.bat"
(back it up first, of course)
1. Look for the following two labels:
:GetWindowsSdkDirHelper32 :GetWindowsSdkDirHelper64
Under each of them, you'll find the following line:
@REM Get windows 10 sdk version number @if not "%WindowsSdkDir%"=="" @FOR /F "delims=" %%i IN ('dir "%WindowsSdkDir%include\" /b /ad-h /on') DO @set WindowsSDKVersion=%%i\
Change it to:
@REM Get windows 10 sdk version number @if not "%WindowsSdkDir%"=="" @FOR /F "delims=" %%i IN ('dir "%WindowsSdkDir%include\" /b /ad-h /on') DO ( @if not "%%i"=="wdf" ( @set WindowsSDKVersion=%%i\ ) )
2. Look for the following two labels:
:GetUniversalCRTSdkDirHelper32 :GetUniversalCRTSdkDirHelper64
Under each of them, change the following line:
@FOR /F "delims=" %%i IN ('dir "%UniversalCRTSdkDir%include\" /b /ad-h /on') DO @SET UCRTVersion=%%i
to:
@FOR /F "delims=" %%i IN ('dir "%UniversalCRTSdkDir%include\" /b /ad-h /on') DO ( @if not "%%i"=="wdf" ( @SET UCRTVersion=%%i ) )
That's it. Let me know if it helped.
Keep in mind that this will skip the
wdf
directories altogether. If the WDK command prompt setup scripts happen to use the samevcvarsqueryregistry.bat
batch file (I doubt it, but...), then they won't work correctly anymore; a bit more hacking will be needed in this case to select the proper batch file for each build environment.这篇关于无法使用Visual Studio 2015命令行工具编译和链接简单的C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!