乐闻世界logo
搜索文章和话题

What is the difference between #include "..." and #include <...>?

1个答案

1

In C++ and C languages, the preprocessor directive #include is used to import or include the content of other files. #include can be used in two different ways: #include ".." and #include <...>.

#include ".."

When using the double-quoted "..." form, the preprocessor first searches for the specified file in the relative path of the source file. If not found, it then searches in the compiler-defined standard library path. This form is typically used for including user-defined header files.

Example:

Assume you have a project with a custom module in the file utils.h. You would typically include it as follows:

cpp
#include "utils.h"

This instructs the preprocessor to first search for utils.h in the current directory (or the relative path specified by the source file).

#include <...>

When using the angle-bracket <...> form, the preprocessor does not search in the relative path; instead, it directly searches in the standard library path for the file. This form is typically used for including standard library header files or third-party library header files.

Example:

When you need to include the iostream header file from the standard library, you would write:

cpp
#include <iostream>

This instructs the preprocessor to search for the iostream file in the system's standard library path.

Summary

In summary, the choice between using double quotes or angle brackets depends on the source of the header file. For user-defined or project internal header files, use double quotes; for system or standard library header files, use angle brackets. This approach not only improves compilation efficiency but also enhances the portability and maintainability of the code.

2024年7月20日 00:29 回复

你的答案