include包含文件查找的顺序 .

简介: 从microsoft网站上找到关于#include Directive (C/C++)的相关问题解释如下: The #include directive tells the preprocessor to treat the contents of a specified file as if t...

从microsoft网站上找到关于#include Directive (C/C++)的相关问题解释如下:

The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears.


Syntax Form

Action

Quoted form

The preprocessor searches for include files in the following order:

  1. In the same directory as the file that contains the #include statement.

  2. In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.

  3. Along the path specified by each /I compiler option.

  4. Along the paths specified by the INCLUDE environment variable.

Angle-bracket form

The preprocessor searches for include files in the following order:

  1. Along the path specified by each /I compiler option.

  2. When compiling from the command line, along the paths that are specified by the INCLUDE environment variable.

按照上述,二者的区别在于:当被include的文件路径不是绝对路径的时候,有不同的搜索顺序。对于使用双引号“”包含的include文件,搜索的时候按以下顺序:

 

1.在包含当前include指令的文件所在的文件夹内搜索;

2.如果上一步找不到,则在之前已经使用include指令打开过的文件所在的文件夹内搜索,如果已经有多个被include的文件,则按照它们被打开的相反顺序搜索;

3.如果上一步找不到,则在编译器设置的include路径内搜索;

4.如果上一步找不到,则在系统的INCLUDE环境变量内搜索。

而对于使用半角尖括号<>包含的include文件,搜索的时候按以下顺序:

1.在编译器设置的include路径内搜索;

2.如果是在命令行中编译,则在系统的INCLUDE环境变量内搜索。

对于非绝对路径的文件使用上述两种include指令搜索时,一旦找到include命令所指定的文件,编译器就停止搜索。但是如果被include的文件是绝对路径的文件,比如 #include "D:\Program Files\OpenCV1.0\cv\include\cv.h" ,被包含的cv.h文件路径是绝对路径,这种情况下编译器直接按照这个给出的绝对路径是搜索。

以下为一个使用尖括号<>include的例子:

#include <stdio.h>

在这个例子里,我们向源程序代码中包含stdio.h文件,由于使用的是尖括号<>,预处理器搜索的时候,先在编译器设置的include路径内搜索,如果找不到,就在系统的INCLUDE环境变量内搜索。

以下为一个使用双引号" "include的例子:

    #include "defs.h"
	在这个例子里,我们向源程序代码中包含defs.h文件,由于使用的是双引号" ",预处理器搜索的时候,使用了这条指令的父文件所在文件夹内搜索,所谓的父文件,就是这条include指令所在的文件,如果找不到,在父文件的父文件所在文件夹内搜索,如果仍然找不到,则在编译器设置的include路径内搜索,如果还找不到,就在系统的INCLUDE环境变量内搜索。

 

 

from:http://blog.csdn.net/godenlove007/article/details/7531521

目录
相关文章
|
7月前
|
Kubernetes 持续交付 开发工具
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
阿里云协同万兴科技落地ACK One GitOps方案,全球多机房应用自动化发布,效率提升50%
189 2
|
编译器 C语言 C++
CMake基础(7)编译标志
CMake基础(7)编译标志
786 0
|
开发工具 git
Vcpkg安装指定版本包或自定义安装包
Vcpkg安装指定版本包或自定义安装包
2089 0
|
编解码 Linux
FFmpeg开发笔记(十)Linux环境给FFmpeg集成vorbis和amr
在Linux环境下,为FFmpeg添加对AAC、MP3、OGG和AMR音频格式的支持,需安装libogg、libvorbis和opencore-amr库。首先,从官方源下载各库的最新源码,如libogg-1.3.5、libvorbis-1.3.7和opencore-amr-0.1.6,然后解压并依次执行`./configure`、`make`和`make install`进行编译安装。接着,在FFmpeg源码目录中,使用`./configure`命令重新配置,并重新编译安装FFmpeg。最后,验证FFmpeg版本信息确认已启用ogg和amr支持。
476 0
FFmpeg开发笔记(十)Linux环境给FFmpeg集成vorbis和amr
|
存储 开发工具 git
如何构建 Protocol Buffers(protobuf)并解决常见问题
如何构建 Protocol Buffers(protobuf)并解决常见问题
384 0
|
NoSQL IDE Unix
Windows下如何使用和调试GDB
Windows下如何使用和调试GDB
Windows下如何使用和调试GDB
|
数据安全/隐私保护 安全
单点登录(SSO)看这一篇就够了
背景 在企业发展初期,企业使用的系统很少,通常一个或者两个,每个系统都有自己的登录模块,运营人员每天用自己的账号登录,很方便。但随着企业的发展,用到的系统随之增多,运营人员在操作不同的系统时,需要多次登录,而且每个系统的账号都不一样,这对于运营人员来说,很不方便。
275399 15
|
存储 程序员 Linux
再谈应用程序分段:代码段、数据段、bss以及堆和栈
再谈应用程序分段:代码段、数据段、bss以及堆和栈
1458 0
|
弹性计算 运维 Kubernetes
|
关系型数据库 MySQL Serverless
serverless devs环境下搭建mysql
serverless devs环境下搭建mysql
581 0
serverless devs环境下搭建mysql