Visual Studio 在哪里查找 C++ 头文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/335408/
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-27 14:45:00  来源:igfitidea点击:

Where does Visual Studio look for C++ header files?

c++visual-studioheader

提问by Brian Sullivan

I checked out a copy of a C++ application from SourceForge (HoboCopy, if you're curious) and tried to compile it.

我从 SourceForge(HoboCopy,如果你好奇的话)查看了一个 C++ 应用程序的副本并试图编译它。

Visual Studio tells me that it can't find a particular header file. I found the file in the source tree, but where do I need to put it, so that it will be found when compiling?

Visual Studio 告诉我它找不到特定的头文件。我在源码树中找到了这个文件,但是我需要把它放在哪里,以便编译时找到它?

Are there special directories?

有特殊目录吗?

回答by Rob Prouse

Visual Studio looks for headers in this order:

Visual Studio 按以下顺序查找标头:

  • In the current source directory.
  • In the Additional Include Directories in the project properties (Project-> [project name] Properties, under C/C++ | General).
  • In the Visual Studio C++ Include directoriesunder ToolsOptionsProjects and SolutionsVC++ Directories.
  • In new versions of Visual Studio (2015+) the above option is deprecated and a list of default include directories is available at Project PropertiesConfigurationVC++ Directories
  • 在当前源目录中。
  • 在项目属性中的附加包含目录中(Project-> [project name] Properties,在 C/C++ | General 下)。
  • 在 Visual Studio C++ Include 目录ToolsOptionsProjects and SolutionsVC++ Directories
  • 在新版本的 Visual Studio (2015+) 中,上述选项已弃用,默认包含目录列表可在项目属性配置VC++ 目录中找到

In your case, add the directory that the header is to the project properties (Project PropertiesConfigurationC/C++GeneralAdditional Include Directories).

在您的情况下,将标题所在的目录添加到项目属性(项目属性配置C/C++常规附加包含目录)。

回答by linrongbin

Actually On my windows 10 with visual studio 2017 community, the C++ headers path are:

实际上在我的带有 Visual Studio 2017 社区的 Windows 10 上,C++ 头文件路径是:

  1. C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include

  2. C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt

  1. C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include

  2. C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt

The 1st contains standard C++ headers such as <iostream>, <algorithm>. The 2nd contains old C headers such as <stdio.h>, <string.h>. The version number can be different based on your software.

第一个包含标准 C++ 头文件,例如<iostream>, <algorithm>。第二个包含旧的 C 头文件,例如<stdio.h>, <string.h>。版本号可能因您的软件而异。

Hope this would help.

希望这会有所帮助。

回答by Adam Rosenfield

If the project came with a Visual Studio project file, then that should already be configured to find the headers for you. If not, you'll have to add the include file directory to the project settings by right-clicking the project and selecting Properties, clicking on "C/C++", and adding the directory containing the include files to the "Additional Include Directories" edit box.

如果项目带有 Visual Studio 项目文件,那么应该已经配置为为您查找标头。如果没有,您必须通过右键单击项目并选择“属性”,单击“C/C++”,然后将包含包含文件的目录添加到“其他包含目录”来将包含文件目录添加到项目设置中编辑框。

回答by SteveWilkinson

Tried to add this as a comment to Rob Prouse's posting, but the lack of formatting made it unintelligible.

试图将此作为评论添加到Rob Prouse的帖子中,但缺乏格式使其难以理解。

In Visual Studio 2010, the "Tools | Options | Projects and Solutions | VC++ Directories" dialog reports that "VC++ Directories editing in Tools > Options has been deprecated", proposing that you use the rather counter-intuitive Property Manager.

在 Visual Studio 2010 中,“工具 | 选项 | 项目和解决方案 | VC++ 目录”对话框报告“工具 > 选项中的 VC++ 目录编辑已被弃用”,建议您使用相当违反直觉的属性管理器。

If you really, really want to update the default $(IncludePath), you have to hack the appropriate entry in one of the XML files:

如果您真的,真的想更新默认的 $(IncludePath),您必须修改 XML 文件之一中的相应条目:

\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\PlatformToolsets\v100\Microsoft.Cpp.Win32.v100.props

\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\PlatformToolsets\v100\Microsoft.Cpp.Win32.v100.props

or

或者

\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\x64\PlatformToolsets\v100\Microsoft.Cpp.X64.v100.props

\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\x64\PlatformToolsets\v100\Microsoft.Cpp.X64.v100.props

(Probably not Microsoft-recommended.)

(可能不是微软推荐的。)

回答by Markus

There seems to be a bug in Visual Studio 2015 community. For a 64-bit project, the include folder isn't found unless it's in the win32 bit configuration Additional Include Folderslist.

Visual Studio 2015 社区中似乎存在一个错误。对于 64 位项目,除非在 win32 位配置附加包含文件夹列表中,否则找不到包含文件夹

回答by Pablo el Puro

There exists a newer question what is hitting the problem better asking How do include paths work in Visual Studio?

存在一个较新的问题是什么更好地解决了问题,包括路径如何在 Visual Studio 中工作?

There is getting revealed the way to do it in the newer versions of VisualStudio

在较新版本的 VisualStudio 中揭示了执行此操作的方法

  • in the current project only(as the question is set here too) as well as
  • for every new project as default
  • 仅在当前项目中(因为这里也设置了问题)以及
  • 默认为每个新项目

The second is the what the answer of Steve Wilkinson above explains, what is, as he supposed himself, not the what Microsoft would recommend.

第二个是上面史蒂夫威尔金森的答案所解释的,正如他自己所认为的那样,而不是微软会推荐的。

To say it the shortway here: do it, but do it in the User-Directory at

在这里说它的捷径:做它,但在用户目录中做

C:\Users\UserName\AppData\Local\Microsoft\MSBuild\v4.0

C:\Users\UserName\AppData\Local\Microsoft\MSBuild\v4.0

in the XML-file

在 XML 文件中

Microsoft.Cpp.Win32.user.props

Microsoft.Cpp.Win32.user.props

and/or

和/或

Microsoft.Cpp.x64.user.props

Microsoft.Cpp.x64.user.props

and not in the C:\program files - directory, where the unmodified Factory-File of Microsoft is expected to reside.

而不是在 C:\program files - 目录中,预计 Microsoft 的未修改工厂文件将驻留在该目录中。

Then you do it the way as VisualStudio is doing it too and everything is regular.

然后你按照 VisualStudio 的方式来做,一切都是正常的。

For more info why to do it alike, see my answer there.

欲了解更多信息,为什么这样做一样,看到我的回答