C++ Visual Studio 2015 在创建一个简单的测试控制台程序时给我错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31736361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:01:32  来源:igfitidea点击:

Visual studio 2015 gives me errors upon creating a simple test console program

c++

提问by Harpo

Here is the code I am using.

这是我正在使用的代码。

#include "stdafx.h"
#include <iostream>

int main() {
    std::cout << "hi";

    return 0;
}

When I create simple c++ console application and try to build it, this error occurs:

当我创建简单的 C++ 控制台应用程序并尝试构建它时,会发生此错误:

cannot open include file 'stdio.h': No such file or directory

Why? Shouldn't stdio.h be included as a standard library? What can I do to get it back?

为什么?不应该将 stdio.h 作为标准库包含在内吗?我该怎么做才能取回它?

edit: I have just looked into C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include directory. There is no stdio.h or stdafx.h . I really am not sure why. How can I get them back?

编辑:我刚刚查看了 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include 目录。没有 stdio.h 或 stdafx.h 。我真的不知道为什么。我怎样才能让他们回来?

回答by Cezar Azevedo de Faveri

That's because Visual studiochanged the path to C headers.

那是因为Visual Studio更改了 C 头文件的路径。

There you have the info about that: https://blogs.msdn.microsoft.com/vcblog/2015/03/03/introducing-the-universal-crt/

你有关于那个的信息:https: //blogs.msdn.microsoft.com/vcblog/2015/03/03/introducing-the-universal-crt/

What i did to solve this is:

我为解决这个问题所做的是:

Go to Project->Properties->. In Configuraton Properties->VC++ Diretories->Library Directoriesadd a path to C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10150.0\ucrt\(Choose your architecture)

Project->Properties->。在Configuraton Properties->VC++ Diretories->Library Directories添加路径C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10150.0\ucrt\(选择你的架构)

And in C/C++->General->Additional include directoriesadd a path to:

C/C++->General->Additional include directories添加一个路径:

C:\Program Files (x86)\Windows Kits\Include.0.10150.0\ucrt

Note: The 10.0.10150.0may vary depending on your version.

注意:10.0.10150.0可能因您的版本而异。

回答by Sean Werkema

I had a similar problem upgrading an existing C project from Visual Studio 2013 to VS2017 (I'd skipped VS2015); none of the standard headers were found there either.

我在将现有的 C 项目从 Visual Studio 2013 升级到 VS2017 时遇到了类似的问题(我跳过了 VS2015);那里也没有找到标准标题。

The accepted answer (by Cezar Azevedo de Faveri) did work for me, but it's inelegant to just jam an absolute path in the settings, especially considering someone can change the install path of both Visual Studio and the SDKs; I'd like to write code that "just works" where possible.

接受的答案(由 Cezar Azevedo de Faveri 提供)确实对我有用,但是在设置中插入绝对路径是不雅的,尤其是考虑到有人可以更改 Visual Studio 和 SDK 的安装路径;我想在可能的情况下编写“正常工作”的代码。

So I spent a little time studying how VS2017 generates a new project, and I eventually found an answer, which is that when VS2017 upgrades an existing C project, it forgets to upgrade one critical project value, and that incorrect value — the Windows SDK Version — makes the headers unable to be found:

于是我花了一点时间研究VS2017如何生成一个新项目,最终找到了一个答案,就是VS2017升级一个现有的C项目时,忘记升级一个关键的项目值,那个不正确的值——Windows SDK Version — 使标题无法找到:

Project Property Pages

Project Property Pages

By default, VS2017 installs the headers only for the Windows 10 UWP SDK, but it doesn't change the "Windows SDK Version" in any projects it upgrades to a version of the SDK that was actually installed! Mine were set to "8.1" after the upgrade, and there are no headers installed for Windows 8.1

默认情况下,VS2017 仅为 Windows 10 UWP SDK 安装标头,但它不会更改任何项目中的“Windows SDK 版本”,它升级到实际安装的 SDK 版本!升级后我的设置为“8.1”,并且没有为 Windows 8.1 安装头文件

So if you're upgrading an existing project, you'll have to change this setting manually to whichever version of the headers you actually have: In my case, that was by explicitly adding 10.0.14393.0to the list (that's the version number for the Windows 10 UWP SDK headers that come with VS2017).

因此,如果您要升级现有项目,则必须手动将此设置更改为您实际拥有的任何版本的标头:在我的情况下,这是通过显式添加10.0.14393.0到列表(这是 Windows 10 的版本号) VS2017 附带的 UWP SDK 标头)。

(The list of installed versions can be found in the C:\Program Files (x86)\Windows Kits\10\Includefolder, and in the similar folders near it.)

(已安装版本的列表可以在该C:\Program Files (x86)\Windows Kits\10\Include文件夹及其附近的类似文件夹中找到。)

回答by XZ6H

I know I am a bit late to this but instead of messing with the path settings, in Visual Studio 2017 you can

我知道我对此有点晚了,但不要弄乱路径设置,在 Visual Studio 2017 中,您可以

  • right-click the project
  • select retarget projects
  • select the latest or any new version of windows SDK and click OK
  • 右键单击项目
  • 选择重定向项目
  • 选择最新或任何新版本的 windows SDK,然后单击“确定”

This will automatically take care of all include paths and libraries.

这将自动处理所有包含路径和库。

回答by Signa

Above there is provided solution is per project. But if you don't want to reinstall VS from scratch or set the include directories and libraries on every solution you can modify Toolset.propsfound in:

上面提供了每个项目的解决方案。但是,如果您不想从头开始重新安装 VS 或在每个解决方案上设置包含目录和库,您可以修改以下中的Toolset.props

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140\Toolset.props

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140\Toolset.props

  <PropertyGroup>
....................
    <IncludePath Condition="'$(IncludePath)' == ''">$(VC_IncludePath);$(WindowsSDK_IncludePath);**C:\Program Files (x86)\Windows Kits\Include.0.10150.0\ucrt**</IncludePath>
.......................
    <LibraryPath Condition="'$(LibraryPath)' == ''">$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;**C:\Program Files (x86)\Windows Kits\Lib.0.10150.0\ucrt\**</LibraryPath>
...........................
  </PropertyGroup>

回答by imallett

#include "stdafx.h"

#include "stdafx.h"

There is a well-known difference between the <...>and "..."includes: briefly, that the former is for library includes and the latter is for local includes.

<...>"..."包含之间有一个众所周知的区别:简单地说,前者用于库包含,后者用于本地包含。

You mention that you were looking around for stdafx.hbut couldn't find it in the compiler installation. This suggests that:

您提到您正在四处寻找,stdafx.h但在编译器安装中找不到它。这表明:

  1. You think stdafx.his a library file (it is not, unless it's some MS-specific extension, which I doubt, although it is traditionally used as a default filename for precompiled headers by the same--if you have made one, which you almost certainly haven't).

  2. Because of 1., you haven't made a local file stdafx.h, and therefore this include directive shouldfail. If it hasn't, then something fishy is happening.

  1. 你认为stdafx.h是一个库文件(它不是,除非它是一些特定于 MS 的扩展名,我对此表示怀疑,尽管它传统上被用作预编译头的默认文件名 -如果你已经做了一个,你几乎可以肯定没有)。

  2. 由于 1.,您还没有创建本地文件stdafx.h,因此这个包含指令应该失败。如果没有,那么就会发生一些可疑的事情。



As to your actual problem, I have some notes:

至于你的实际问题,我有一些说明:

  1. <stdio.h>is the C header, not the C++ one. If you're including from a C++ file (extension .cpp, probably, for MSVC), then you should use the C++ header <cstdio>. However, this shouldn't actually cause the problem.

  2. You aren't using the stdio anyway (at least not directly). You're using iostream, which you're properly including. If that include is the one that's causing the error, then iostream is trying to include it, can't, and your compiler installation is borked.

  3. Try the similar program:

  1. <stdio.h>是 C 头文件,而不是 C++ 头文件。如果您从 C++ 文件(扩展名.cpp,可能是 MSVC)中包含,那么您应该使用 C++ header <cstdio>。但是,这实际上不应该导致问题。

  2. 无论如何您都没有使用 stdio(至少不是直接使用)。您正在使用 iostream,您正确地包含了它。如果该包含是导致错误的那个,那么 iostream 正在尝试包含它,但不能,并且您的编译器安装失败。

  3. 试试类似的程序:



#include <iostream>
int main() {
    std::cout << "hi" << std::endl;
    return 0;
}

I have just checked myself that this compiles and executes properly under Visual Studio 2015 Professional.

我刚刚检查过自己是否可以在 Visual Studio 2015 Professional 下正确编译和执行。

If this program does notcompile, I suggest reinstalling Visual Studio. In my experience, this often fixes these tricky setup issues.

如果这个程序不能编译,我建议重新安装Visual Studio。根据我的经验,这通常可以解决这些棘手的设置问题。

回答by Arpit Jaiswal

I faced the same issue , it got resolved when I ran vcvarsall.batwhich is present at C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC

我遇到了同样的问题,当我运行存在于C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC 的vcvarsall.bat时它得到了解决

回答by muusbolla

I had this error on VS2017 after upgrading from VS2015. I tried a clean + reinstall and it did not fix the error. The problem that I found was two-fold:

从 VS2015 升级后,我在 VS2017 上遇到此错误。我尝试了干净+重新安装,但没有解决错误。我发现的问题有两个:

  1. $(VC_IncludePath);$(WindowsSDK_IncludePath); was not in the default include path.
  2. $(VC_IncludePath);$(WindowsSDK_IncludePath); was actually EXCLUDED in the default properties (how did this happen?!)
  1. $(VC_IncludePath);$(WindowsSDK_IncludePath); 不在默认的包含路径中。
  2. $(VC_IncludePath);$(WindowsSDK_IncludePath); 实际上在默认属性中被排除(这是怎么发生的?!)

To fix for new projects: Manually edit the following files: %LOCALAPPDATA%\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props %LOCALAPPDATA%\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props

修复新项目:手动编辑以下文件:%LOCALAPPDATA%\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props %LOCALAPPDATA%\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64 .user.props

Make sure that $(VC_IncludePath);$(WindowsSDK_IncludePath); is in the IncludePath and NOT in the ExcludePath.

确保 $(VC_IncludePath);$(WindowsSDK_IncludePath); 在 IncludePath 中而不在 ExcludePath 中。

To fix for old projects (that don't just inherit from the above files): Manually edit your project properties in the Solution Explorer and make sure that $(VC_IncludePath);$(WindowsSDK_IncludePath); is in the IncludePath and NOT in the ExcludePath.

修复旧项目(不只是从上述文件继承):在解决方案资源管理器中手动编辑您的项目属性并确保 $(VC_IncludePath);$(WindowsSDK_IncludePath); 在 IncludePath 中而不在 ExcludePath 中。

回答by Noxoreos

I had the same problem in Visual Studio Community 2015 after installing the current Windows SDK and as Signa already wrote earlier, this can be fixed for all projects within a "Toolset.props" file (at least for VS2015) and I find this to be the most convenient solution, because this has to be done only once. I've got a few side notes, because there is something to watch out for.

在安装当前的 Windows SDK 后,我在 Visual Studio Community 2015 中遇到了同样的问题,正如 Signa 之前已经写过的,这可以修复“Toolset.props”文件中的所有项目(至少对于 VS2015),我发现这是最方便的解决方案,因为这只需要做一次。我有一些旁注,因为有一些事情需要注意。

For each build platform there is an own "Toolset.props" file, so both need to be modified if you want to build for 32 and 64 bit targets:

对于每个构建平台,都有一个自己的“ Toolset.props”文件,因此如果要为 32 位和 64 位目标构建,则需要修改两者:

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140\Toolset.props

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\x64\PlatformToolsets\v140\Toolset.props

The files are write-protected and you need to remove the write protection before you can change those files (remember to put it back on after you're done).

这些文件是写保护的,您需要先取消写保护,然后才能更改这些文件(请记住在完成后将其重新打开)。

As of now the current SDK version is "10.0.15063.0" and you need to adjust that to the version you want to use (or to the SDK version you have installed).

截至目前,当前的 SDK 版本为“ 10.0.15063.0”,您需要将其调整为您要使用的版本(或您已安装的 SDK 版本)。

Look out for the IncludePath and LibraryPath lines in those props files and add the following paths to them:

注意这些 props 文件中的 IncludePath 和 LibraryPath 行,并向它们添加以下路径:

IncludePath:$(ProgramFiles)\Windows Kits\10\Include\10.0.15063.0\ucrt

LibraryPath:$(ProgramFiles)\Windows Kits\10\Lib\10.0.15063.0\ucrt\$(PlatformTarget)

包含路径$(ProgramFiles)\Windows Kits\10\Include\10.0.15063.0\ucrt

库路径$(ProgramFiles)\Windows Kits\10\Lib\10.0.15063.0\ucrt\$(PlatformTarget)

Here a sample how this looks like for the 32 bit version:

这是 32 位版本的示例:

// ... some XML before that ...
<PropertyGroup>
    // ... executable path .....
    <IncludePath Condition="'$(IncludePath)' == ''">$(VC_IncludePath);$(WindowsSDK_IncludePath);$(ProgramFiles)\Windows Kits\Include.0.15063.0\ucrt;</IncludePath>
    // ... reference path ...
    <LibraryPath Condition="'$(LibraryPath)' == ''">$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);$(NETFXKitsDir)Lib\um\x86;$(ProgramFiles)\Windows Kits\Lib.0.15063.0\ucrt$(PlatformTarget);</LibraryPath>
    // ... more XML ...
</PropertyGroup>
// ... even more XML ....

回答by Micha Zf

After running in similar problems once more with VS2017 I took a closer look at what caused all this. And the main reason was that I was still using modified user.props files. Which was for a while a solution to add global include and library paths to all projects. But this feature is deprecated by Microsoft and the content of those files should be reset.

在使用 VS2017 再次遇到类似问题后,我仔细研究了导致这一切的原因。主要原因是我仍在使用修改过的 user.props 文件。有一段时间是向所有项目添加全局包含和库路径的解决方案。但是此功能已被 Microsoft 弃用,应重置这些文件的内容。

The files I'm talking about are the user.props files in C:\Users\your_name\AppData\Local\Microsoft\MSBuild\v4.0 For testing you can simply rename (or delete if you like risks) them and restart VS. It will create empty files for those now. And if you are on Windows 10 then in most cases this is already enough to fix all your problems. Even in older VS versions (I tested with VS2010-VS2017, for even older VS versions the troubles tend to involve registry keys and don't involve this props files). Windows/VS has become now really good at finding all the system libraries (including DirectX which was the main reason we had to modify those files in the past) and adding them in the correct include order.

我正在谈论的文件是 C:\Users\your_name\AppData\Local\Microsoft\MSBuild\v4.0 中的 user.props 文件为了测试,您可以简单地重命名(或删除,如果您喜欢风险)并重新启动 VS . 它现在将为那些创建空文件。如果您使用的是 Windows 10,那么在大多数情况下,这已经足以解决您的所有问题。即使在较旧的 VS 版本中(我使用 VS2010-VS2017 进行了测试,对于更旧的 VS 版本,问题往往涉及注册表项,而不涉及此道具文件)。Windows/VS 现在非常擅长查找所有系统库(包括 DirectX,这是我们过去必须修改这些文件的主要原因)并以正确的包含顺序添加它们。

Also a warning as I've seen other people recomment that. Do notchange any .prop installed by the SDK. If you really need to work with props then create and add your own property sheets (which can overwrite any defaults) to your project. And don't worry, those will not be checked in to source-control so you can still distribute your project to others.

也是一个警告,因为我已经看到其他人对此进行了评论。不要改变由SDK安装的任何.prop。如果你真的需要使用 props,那么创建并添加你自己的属性表(它可以覆盖任何默认值)到你的项目中。不用担心,这些不会被签入源代码管理,因此您仍然可以将您的项目分发给其他人。

If you are still on an older Windows it might not be as easy as in Windows 10, but I'll try to give some hints:

如果您仍在使用较旧的 Windows,它可能不像在 Windows 10 中那么容易,但我会尝试给出一些提示:

What you are missing for that concrete error is the new $UniversalCRT_IncludePath. No need to hardcode that path, that macro should contain the correct one. So add $(UniversalCRT_IncludePath); to the IncludePath in your own property which you add then to the project.

对于那个具体错误,您缺少的是新的 $UniversalCRT_IncludePath。无需对该路径进行硬编码,该宏应包含正确的路径。所以添加 $(UniversalCRT_IncludePath); 到您自己的属性中的 IncludePath,然后将其添加到项目中。

And for LibraryPath add the correct path per platform-file, like $(UniversalCRT_LibraryPath_x64); for .x64. and $(UniversalCRT_LibraryPath_x86); for .Win32.

对于 LibraryPath,为每个平台文件添加正确的路径,例如 $(UniversalCRT_LibraryPath_x64); 对于 .x64。和 $(UniversalCRT_LibraryPath_x86); 对于 .Win32。

What also might be useful when trying to fix this: You can find out the values of all the $(MACRO) variables used in the build system inside VisualStudio. They are just very well hidden: Go in properties - custom build steps - click on command line - then don't type anything but click the down button to get "edit..." - you click that - you get a dialog which has a "Macros>>" button. And thatcontains a list with all macro values.

尝试修复此问题时还有什么用处:您可以在 VisualStudio 中找出构建系统中使用的所有 $(MACRO) 变量的值。它们隐藏得非常好:进入属性 - 自定义构建步骤 - 单击命令行 - 然后不要输入任何内容,但单击向下按钮以获取“编辑...” - 单击它 - 你会得到一个对话框“宏>>”按钮。而包含了所有的宏值的列表。

回答by Eduardo Hernández

Installing Visual Studio 2015 Update 3 solves this issue, both for new projects and for existing projects created before the update.

安装 Visual Studio 2015 Update 3 可以解决此问题,无论是新项目还是更新前创建的现有项目。

https://www.visualstudio.com/news/releasenotes/vs2015-update3-vs

https://www.visualstudio.com/news/releasenotes/vs2015-update3-vs