简体   繁体   English

Visual Studio C ++包含的包含文件的包含路径顺序

[英]Visual Studio C++ include path order for nested include files

I found the article Where does Visual Studio look for C++ header files? 我找到了文章Visual Studio在哪里寻找C ++头文件? to be a good start, but I have further questions. 是个好的开始,但我还有其他问题。 The general order that VS looks for include files are (1) local directory, (2) those specified with /I, and (3) those specified by the environment (INCLUDE env var or VC++ settings). VS查找包含文件的一般顺序是(1)本地目录,(2)用/ I指定的文件,以及(3)环境指定的文件(INCLUDE env var或VC ++设置)。

Q1. Q1。 I think that the /X option turns off (3). 我认为/ X选项已关闭(3)。 Right? 对? Or does it also turn off (1)? 还是也关闭(1)?

Q2. Q2。 If I have a nested include file (main.c includes inc1.h, which includes inc2.h), where the first included file is found in one of the /I folders, does VS look for the second included file starting in that same /I folder, or just the local folder of the original source file? 如果我有一个嵌套的包含文件(main.c包含inc1.h,其中包括inc2.h),其中第一个包含的文件位于一个/ I文件夹中,那么VS是否会从该文件中查找第二个包含的文件/ I文件夹,还是原始源文件的本地文件夹? VS2008 seems to be operating the first way, but I'd like to find it documented somewhere. VS2008似乎是第一种方式,但是我想在某个地方找到它的文档。

The /X option will not disable looking for files in the local directory (assuming you are including them like "this.h" rather than <this.h> ). /X选项不会禁止在本地目录中查找文件(假设您将其包含为"this.h"而不是<this.h> )。 You can easily test this by creating a file 您可以通过创建文件来轻松测试

  #include "foo.h"
  int main() {}

creating an empty foo.h , and compiling using the /X flag. 创建一个空的foo.h ,并使用/X标志进行编译。

For Q2, my test with VC2010 showed it behaves the same as VC2008. 对于第二季度,我对VC2010的测试表明它的行为与VC2008相同。 What I did was had a main: 我所做的主要是:

  #include "inc1.h"
  int main() {}

with a folder inc that contained an inc1.h that just 与包含inc1.h的文件夹inc

  #include "inc2.h"

with two different inc2.h files; 具有两个不同的inc2.h文件; on in inc and one in the directory with my source file. inc和我的源文件的目录中。 The one in my source directory was empty, the one in inc had an #error directive. 我的源目录中的一个是空的, inc中的一个是一个#error指令。 In general, you don't want to rely on this though. 通常,您不想依赖于此。 Both the C and C++ standards simply say on #include that "the named source file is searched for in an implementation-defined manner". C和C ++标准都简单地在#include说“以实现定义的方式搜索命名的源文件”。

Q1. Q1。 The /X option does turn off the INCLUDE var, but not the local directory (see Q2) /X选项不会关闭INCLUDE var,但不会关闭本地目录(请参阅Q2)

Q2. Q2。 C and C++ compilers, for nested include files, make the location of an include file the current local directory, so you can't turn it off. 对于嵌套的包含文件,C和C ++编译器将包含文件的位置设置为当前本地目录,因此无法将其关闭。

eg. 例如。 If your inc.h file is found in one of the /I folders and it tries to include a file like this: 如果在/I文件夹之一中找到您的inc.h文件,并且它尝试包含这样的文件:
#include "foo/foo.h" then it must use its own local directory first, before all the other /I folders as they all might have a foo folder with a foo.h file and your inc.h file would probably not compile as its just included the wrong header file. #include "foo/foo.h"那么它必须先使用其自己的本地目录,然后再使用所有其他/I文件夹,因为它们可能都包含带有foo.h文件的foo文件夹,而您的inc.h文件可能无法编译因为它只是包含了错误的头文件。

I've just found this msdn page for VS2008 which seems to have a complete explanation. 我刚刚找到了VS2008的msdn页面 ,其中似乎有完整的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM