C++ 如何在 Visual Studio 中包含子目录?

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

How to include sub-directories in Visual Studio?

c++visual-studiovisual-studio-2005include

提问by kklobucki

I have to include many header files, which are in different sub-directories. Is there a way in Visual Studio (I am using 2005 edition) to set one include path that Visual Studio will search also the sub-directories for header files?

我必须包含许多头文件,它们位于不同的子目录中。在 Visual Studio(我使用的是 2005 版)中是否有一种方法可以设置一个包含路径,Visual Studio 还将在子目录中搜索头文件?

回答by Joris Timmermans

Setting the folder search paths in the Visual Studio settings to fix an include issue is generally not really a good idea from a design point of view. Your code will be less portable to different machines with different directory lay-outs.

从设计的角度来看,在 Visual Studio 设置中设置文件夹搜索路径来修复包含问题通常不是一个好主意。您的代码在具有不同目录布局的不同机器上的可移植性较差。

My suggestion would be to settle on an organisation of your code so that the relative paths of the sub-directories (relative to your including code) are fixed:

我的建议是确定代码的组织方式,以便固定子目录的相对路径(相对于包含的代码):

  • Add the "base folder" to the project (project properties -> Configuration Properties -> C/C++ -> Additional Include Directories, "additional include directories")
  • Add the subdirectories to the #include statements , i.e. #include "subdirectory/somefile.h".
  • 将“基本文件夹”添加到项目中(项目属性 -> 配置属性 -> C/C++ -> 附加包含目录,“附加包含目录”
  • 将子目录添加到#include 语句,即#include "subdirectory/somefile.h"

This has the added bonus of being able to see which folder in your Solution contains the file - that is often useful information when you're trying to find your way around or trying to figure out what a file is for.

这有一个额外的好处,即能够查看解决方案中的哪个文件夹包含该文件 - 当您试图找到自己的方法或试图找出文件的用途时,这通常是有用的信息。

回答by David Sykes

We haven't found a way of setting a recursive folder search in Visual Studio (though Xcode has no problem setting and using them). What we do is list all the directories in a file, and specify the file in the C/C++ Command Line additional options:

我们还没有找到在 Visual Studio 中设置递归文件夹搜索的方法(尽管 Xcode 设置和使用它们没有问题)。我们所做的是列出文件中的所有目录,并在 C/C++ 命令行附加选项中指定该文件:

@"IncludeFolders.rsp"

@"包含文件夹.rsp"

We rarely find problems using recursive search, though an accidental inclusion of ``/'' did slow things down a bit once.

我们很少使用递归搜索发现问题,尽管偶然包含“/”确实会减慢速度。

回答by riderBill

I've found that I can shorten path lengths in most situations, including in MSVC, by a slight of hand trick using the SUBST command. I'll show and example for shortening the default inherited C++ include paths, but you would do this for your project related include paths. First create one or more batch files like this (and run them from Windows Explorer or the command line):

我发现在大多数情况下,包括在 MSVC 中,我可以通过使用 SUBST 命令的一点技巧来缩短路径长度。我将展示缩短默认继承的 C++ 包含路径的示例,但您将对与项目相关的包含路径执行此操作。首先创建一个或多个这样的批处理文件(并从 Windows 资源管理器或命令行运行它们):

MDrive.bat:
subst M: /D
subst M: "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC"

WDrive.bat:
subst W: /D
subst W: "C:\Program Files (x86)\Windows Kits.1\Include"

... This is just and example.  You would do this for your
    project related include directories.

You can create a number of these for your longer paths. In the case above I am only shortening the standard inherited paths that MSVC uses by default. BTW, the Subst command works w/o administrative rights, but don't step on an existing physical or mapped drive letter (I'm not sure what would happen--I don't want to test it at the moment).

您可以为更长的路径创建许多这些。在上述情况下,我只是缩短了 MSVC 默认使用的标准继承路径。顺便说一句,Subst 命令在没有管理权限的情况下工作,但不要踩到现有的物理或映射驱动器号(我不确定会发生什么——我现在不想测试它)。

This step is optional. I in MSVS 2010+ I add a custom properties sheet. BTW, if you don't know about custom properties sheets you should.See Property Pages (Visual C++). VIEW-Other Windows-Property Window, right click the project in the Property manager, Add a New Project Properties Sheet. Under User Macros, add a Macro, e.g. MyProjectIncludePathsand set the value (in this example) to

此步骤是可选的。我在 MSVS 2010+ 中添加了一个自定义属性表。 顺便说一句,如果你不知道自定义属性表,你应该知道。请参阅属性页 (Visual C++)VIEW-Other Windows-Property Window,右键单击 Property manager 中的项目,Add a New Project Properties Sheet。在User Macros 下,添加一个宏,例如MyProjectIncludePaths并将值(在本例中)设置为

M:; M:\atlmfc\include; W:\um; W:\shared; W:\winrt

--only 49 characters in this example, which is much shorter than the default of

--在这个例子中只有49个字符,比默认的短得多

$(VC_IncludePath); $(WindowsSKD_IncludePath)

which upon macro expansion translates to

这在宏扩展时转化为

C:\Program Files (x86)\Windows Kits.1\Include;C:\Program Files (x86)\Windows Kits.1\Include\atlmfc\include;C:\Program Files (x86)\Windows Kits.1\Include\um;C:\Program Files (x86)\Windows Kits.1\Include\shared;C:\Program Files (x86)\Windows Kits.1\Include\winrt

By my count the default is 270 characters--i.e. longer than 260 characters--can anyone explain this?

根据我的统计,默认值是 270 个字符——即超过 260 个字符——谁能解释一下?

In the Property Pages for the project(i.e. not the custom property sheet), in my example I went to Configuration Properties-VC++ Directoriesand changed the Include Directoriesvalue to $(MyProjectIncludePaths), but in practice it you would go to (in your custom property sheet page if you created one) C/C++-Generaland add $(MyProjectIncludePaths)to the Additional Include Directories.

项目的属性页(即不是自定义属性表)中,在我的示例中,我转到Configuration Properties-VC++ Directories并将Include Directories值更改为$(MyProjectIncludePaths),但实际上你会去(在你的自定义属性表页面(如果您创建了一个)C/C++-General并将$(MyProjectIncludePaths)添加到Additional Include Directories

In addition to the inherited 270 character paths, I have been able to add a few reasonable length paths under Additional Include Directories. Maybe the Additional Include Directories has its own length limit (?).

除了继承的 270 个字符路径之外,我还能够在Additional Include Directories下添加一些合理长度的路径。也许附加包含目录有自己的长度限制 (?)。

回答by Tuminoid

I believe using recursive search for include files would cause so much more problems than it solves in a form of wrong files or wrong versions being included. After all, you have to define the right directories once when you set up the project.

我相信使用递归搜索包含文件会导致比以错误文件或错误版本包含的形式解决的问题更多。毕竟,您必须在设置项目时定义一次正确的目录。