What is the difference between #include

2018-12-30 23:58发布

In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include statement, as follows?

  1. #include <filename>
  2. #include "filename"

30条回答
唯独是你
2楼-- · 2018-12-31 00:25

The sequence of characters between < and > uniquely refer to a header, which isn't necessarily a file. Implementations are pretty much free to use the character sequence as they wish. (Mostly, however, just treat it as a file name and do a search in the include path, as the other posts state.)

If the #include "file" form is used, the implementation first looks for a file of the given name, if supported. If not (supported), or if the search fails, the implementation behaves as though the other (#include <file>) form was used.

Also, a third form exists and is used when the #include directive doesn't match either of the forms above. In this form, some basic preprocessing (such as macro expansion) is done on the "operands" of the #include directive, and the result is expected to match one of the two other forms.

查看更多
无与为乐者.
3楼-- · 2018-12-31 00:25

#include <filename>

will find the corresponding file from the C++ library. it means if you have a file called hello.h in the C++ library folder, #include <hello.h> will load it.

But,

#include "filename"

will find the file in the same directory where your source file is.

In addition,

#include "path_to_file/filename"

will find the file in the directory which you typed in path_to_file.

查看更多
深知你不懂我心
4楼-- · 2018-12-31 00:26

the " < filename > " searches in standard C library locations

whereas "filename" searches in the current directory as well.

Ideally, you would use <...> for standard C libraries and "..." for libraries that you write and are present in the current directory.

查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 00:26

The order of search header files is different. <XXX.h> prefer to search the standard headers first while "XXX.h" searches the workspace's header files first.

查看更多
骚的不知所云
6楼-- · 2018-12-31 00:27

The #include <filename> is used when a system file is being referred to. That is a header file that can be found at system default locations like /usr/include or /usr/local/include. For your own files that needs to be included in another program you have to use the #include "filename" syntax.

查看更多
冷夜・残月
7楼-- · 2018-12-31 00:28

For #include "" a compiler normally searches the folder of the file which contains that include and then the other folders. For #include <> the compiler does not search the current file's folder.

查看更多
登录 后发表回答