windows 包含路径如何在 Visual Studio 中工作?

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

How do include paths work in Visual Studio?

windowsvisual-studioinclude

提问by bastibe

Visual Studio drives me crazy and I am suspecting I am doing something wrong.

Visual Studio 让我发疯,我怀疑我做错了什么。

This is what I do: I installed Visual Studio (Pro '08) a long time ago, I installed the Windows SDK (Win 7 x64), someone emails me a project, it fails to build.

这就是我所做的:很久以前我安装了 Visual Studio (Pro '08),我安装了 Windows SDK (Win 7 x64),有人通过电子邮件向我发送了一个项目,但它无法构建。

Invariably, it can not find windows.h. While it is easy enough to include C:\Program Files\Microsoft SDKs\Windows\v7.0\Includein the project settings of every single project, I feel that this may not be the proper way to do it. Is there a way to somehow make Visual Studio globally aware of these files?
I also tried to include the above directory in the pathvariable but that didn't change anything.

总是找不到windows.h。虽然将其包含C:\Program Files\Microsoft SDKs\Windows\v7.0\Include在每个项目的项目设置中很容易,但我觉得这可能不是正确的做法。有没有办法让 Visual Studio 全局感知这些文件?
我还尝试将上述目录包含在path变量中,但这并没有改变任何东西。

Also, it will randomly find windows.h but not winresrc.h in the same directory.

此外,它会在同一目录中随机找到 windows.h 而不是 winresrc.h。

Edit:
It was a problem with my machine. Somehow, Visual Studio thought that my Windows SDK was in the wrong place. See accepted answer and comments for the fix.

编辑:
这是我的机器的问题。不知何故,Visual Studio 认为我的 Windows SDK 放错了地方。请参阅已接受的答案和修复评论。

采纳答案by RichieHindle

This answer only applies to ancient versions of Visual Studio - see the more recent answers for modern versions.

此答案仅适用于 Visual Studio 的旧版本 - 请参阅现代版本的最新答案。

You can set Visual Studio's global include path here:

您可以在此处设置 Visual Studio 的全局包含路径:

Tools/ Options/ Projects and Solutions/ VC++ Directories/ Include files

Tools/ Options/ Projects and Solutions/ VC++ Directories/Include files

回答by stackunderflow

@RichieHindle solution is now deprecated as of Visual Studio 2012. As the VS studio prompt now states:

@RichieHindle 解决方案现已从 Visual Studio 2012 开始弃用。正如 VS Studio 提示现在所述:

VC++ Directories are now available as a user property sheet that is added by default to all projects.

VC++ 目录现在可用作默认添加到所有项目的用户属性表。

To set an include path you now must right-click a project and go to:

要设置包含路径,您现在必须右键单击一个项目并转到:

Properties/VC++ Directories/General/Include Directories

Screenshot: enter image description here

截屏: 在此处输入图片说明

回答by Pablo el Puro

To resume the working solutions in VisualStudio 2013 and 2015 too:

要恢复 VisualStudio 2013 和 2015 中的工作解决方案:

Add an include-path to the current project only

仅向当前项目添加包含路径

In Solution Explorer(a palette-window of the VisualStudio-mainwindow), open the shortcut menu for the project and choose Properties, and then in the left pane of the Property Pages dialog box, expand Configuration Properties and select VC++ Directories. Additional include- or lib-paths are specifyable there.

解决方案资源管理器(VisualStudio 主窗口的调色板窗口)中,打开项目的快捷菜单并选择“属性”,然后在“属性页”对话框的左窗格中,展开“配置属性”并选择“VC++ 目录”。在那里可以指定额外的包含路径或库路径。

Its the what Stackunderflowand user1741137say in the answers above. Its the what Microsoft explains in MSDNtoo.

这是Stackunderflowuser1741137在上面的答案中所说的。它也是微软在MSDN 中解释的。

Add an include-path to every new project automatically

自动为每个新项目添加包含路径

Its the question, what Jay Elstonis asking in a comment above and what is a very obvious and burning question in my eyes, what seems to be nonanswered here yet.

这是一个问题,Jay Elston在上面的评论中提出了什么问题,以及在我眼里什么是一个非常明显和紧迫的问题,这里似乎还没有答案。

There exist regular ways to do it in VisualStudio (see CurlyBrace.com), what in my experience are not working properly. In the sense, that it works only once, and thereafter, it is no more expandable and nomore removable. The approach of Steve Wilkinsonin another close related thread of StackOverflow, editing the Microsoft-Factory-XML-file in the ‘program files' - directory is probably a risky hack, as it isnt expected by Microsoft to meet there something foreign. The effect is potentally unpredictable. Well, I like rather to judge it risky not much, but anyway the best way to make VisualStudio work incomprehensible at least for someone else.

在 VisualStudio 中存在常规方法(请参阅CurlyBrace.com),但根据我的经验,这些方法无法正常工作。从某种意义上说,它只能工作一次,此后不再可扩展,也不再可移动。的做法史蒂夫·威尔金森在StackOverflow上的另一个密切相关的线程,编辑微软工厂XML文件中的“程序文件” -目录可能是一个危险的黑客,因为它不是由微软预计可满足有外国的东西。这种影响可能是不可预测的。好吧,我宁愿判断它风险不大,但无论如何,这是使 VisualStudio 至少对其他人难以理解的最佳方式。

The what is working fine compared to, is the editing the corresponding User-XML-file:

相比之下,工作正常的是编辑相应的 User-XML 文件:

C:\Users\UserName\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props

C:\Users\UserName\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props

or/and

或/和

C:\Users\UserName\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props

C:\Users\UserName\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props

For example:

例如:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets">
  </ImportGroup>
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup>
    <IncludePath>C:\any-name\include;$(IncludePath)</IncludePath>
    <LibraryPath>C:\any-name\lib;$(LibraryPath)</LibraryPath>
  </PropertyGroup>
  <ItemDefinitionGroup />
  <ItemGroup />
</Project>

Where the directory ‘C:\any-name\include' will get prepended to the present include-path and the directory ‘C:\any-name\lib' to the library-path. Here, we can edit it ago in an extending and removing sense and remove it all, removing thewhole content of the tag .

其中目录 'C:\any-name\include' 将被添加到当前的包含路径,目录 'C:\any-name\lib' 将被添加到库路径。在这里,我们可以在扩展和删除意义上进行编辑,然后将其全部删除,删除标签的全部内容。

Its the what makes VisualStudio itself, doing it in the regular way what CurlyBracedescribes. As said, it isnt editable in the CurlyBrace-waythereafter nomore, but in the XML-editing-way it is.

它是 VisualStudio 本身的原因,它以CurlyBrace描述的常规方式进行。如前所述,此后不再可以在CurlyBrace 方式中编辑它,但可以在 XML 编辑方式中进行编辑。

For more insight, see Brian Tyler@MSDN-Blog2009, what is admittedly not very fresh, but always the what Microsoft is linking to.

有关更多见解,请参阅 Brian Tyler@MSDN-Blog2009,这当然不是很新鲜,但始终是 Microsoft 所链接的内容。

回答by Piotr Dobrogost

To use Windows SDK successfully you need not only make include files available to your projects but also library files and executables (tools). To set all these directories you should use WinSDK Configuration Tool.

要成功使用 Windows SDK,您不仅需要使包含文件可用于您的项目,还需要库文件和可执行文件(工具)。要设置所有这些目录,您应该使用WinSDK 配置工具

回答by user1741137

If you are only trying to change the include paths for a project and not for all solutions then in Visual Studio 2008 do this: Right-click on the name of the project in the Solution Navigator. From the popup menu select Properties. In the property pages dialog select Configuration Properties->C/C++/General. Click in the text box next to the "Additional Include Files" label and browse for the appropriate directory. Select OK.

如果您只是尝试更改项目的包含路径,而不是所有解决方案的包含路径,那么在 Visual Studio 2008 中执行以下操作: 在解决方案导航器中右键单击项目名称。从弹出菜单中选择属性。在属性页对话框中选择 Configuration Properties->C/C++/General。单击“其他包含文件”标签旁边的文本框并浏览相应的目录。选择确定。

What annoys me is that some of the answers to the original question asked do not apply to the version of Visual Studio that was mentioned.

令我烦恼的是,原始问题的一些答案不适用于提到的 Visual Studio 版本。

回答by Tomilov Anatoliy

This answer will be useful for those who use a non-standard IDE (i.e. Qt Creator).

这个答案对那些使用非标准 IDE(即 Qt Creator)的人很有用。

There are at least two non-intrusive ways to pass additional include pathsto Visual Studio's cl.exevia environment variables:

至少有两种非侵入式方法可以通过环境变量将额外的包含路径传递给 Visual Studio cl.exe

  • Set INCLUDEenvironment variable to ;-separated list of all include paths. It overrides all includes, inclusive standard library ones. Not recommended.
  • Set CLenvironment variable to the following value: /I C:\Lib\VulkanMemoryAllocator\src /I C:\Lib\gli /I C:\Lib\gli\external, where each argument of /Ikey is additional include path.
  • INCLUDE环境变量设置;为所有包含路径的分隔列表。它覆盖所有包含,包含标准库的。不建议。
  • CL环境变量设置为以下值:/I C:\Lib\VulkanMemoryAllocator\src /I C:\Lib\gli /I C:\Lib\gli\external,其中/Ikey 的每个参数都是附加的include path

I successfully use the last one.

我成功使用了最后一个。

回答by Sam Harwell

You need to make sure and have the following:

您需要确保并具备以下条件:

#include <windows.h>

and not this:

而不是这个:

#include "windows.h"

If that's not the problem, then check RichieHindle's response.

如果这不是问题,请查看RichieHindle 的回复