开发者

Difference between <include.hpp> and "include.hpp" [duplicate]

开发者 https://www.devze.com 2023-04-04 09:02 出处:网络
This question already has answers here: What is the difference between #include <filename> and #include "filename"?
This question already has answers here: What is the difference between #include <filename> and #include "filename"? (30 answers) 开发者_C百科 Closed 8 years ago.

I am new to C++.

What is the difference between including the c++ header files using "" and <>

I am trying to use some of the header files form an open source library. All header files in that library are included using <>. Now when I do the same in my header file, its failing at compile time.


<> looks firstly in the header path for the header file whereas "" looks firstly in the current directory of the file for the header.


The distinction is very largely implementation defined; the "..." form should look first in the place where the file which includes it is situated; the <...> no. Beyond that, both look in an implementation defined list of places, with the additional requirement that if the compiler doesn't find a "..." form in any of the expected places, it reprocesses the include as if it were a <...> form.

In practice, all of the compilers I know build a list of places using the -I or /I options, followed by a number of "standard" and compiler defined places. This list is for <...>; "..." is searched in the same directory as the including file, then treated as a <...>. (Some compilers, at least, also have options to add to the list for "...".)

I'm not sure what's happening with regards to the library. Normally, when using a third party library, you have to add one or more -I or /I options to tell the compiler where to find its headers. Once you've done that, both your code and the library code should find all of the necessary headers. The one case I can think of where an include might work in a library header, and not in your own headers, is a "..." style include in a library header which was included from another library header, using a path specifier, e.g.:

LibraryFile1.hpp:

#include "Subdir/LibraryFile2.hpp"

LibraryFile2.hpp:

#include "LibraryFile3.hpp"

You will have told the compiler to look for the headers (using a -I option) in something like LibraryRoot/include, which is where LibraryFile1.hpp is located; LibraryFile2.hpp is relative to this location, and in LibraryFile2.hpp, the compiler finds LibraryFile3.hpp because it is in the same directory as the file which includes it. If you try to include LibraryFile3.hpp directly, however, the compiler won't find it.


File includes between <> are looked for in your compiler's path, whereas "" is looking relatively to your current directory (or absolute if you specify a path that starts with / or c:\ but this is not recommended)

On Unix systems, by default the path contains /usr/include. This path may be completed by adding -Isome_directory for it to search in it.

For example, if you have your file test.c and you want to include include/test.h, you have different choices:

  • Write #include "include/test.h", which will look relatively from the directory of the compiled file.

  • Write #include <test.h>, but this time you will need to specify -Iinclude to the compiler to add the ./include directory to the compiler's path.

Note, however, that some compilers accept the "" notation for lookups in the path, but that always confused me and is a bad thing to do.


The quotes mean include from local folder and the <> mean to include from another directory specified using a flag to g++ or MSVC or whatever compiler you are using or system headers.


<> looks in the default directory for include files, "" looks in the current directory and than in the default directory


This question is a duplicate of Question 21593. None of the above answers above are totally correct. Like many programmers, I have used the informal convention of using the "myApp.hpp" form for application specific files, and the form for library and compiler system files, i.e. files specified in /I and the INCLUDE environment variable. However, the C standard states that the search order is implementation specific.

Here's the msdn explanation copied here for your convenience).

Quoted form The preprocessor searches for include files in this order:
1. In the same directory as the file that contains the #include statement.
2. In the directories of the currently opened include files, in the reverse order in which
they were opened. The search begins in the directory of the parent include file and
continues upward through the directories of any grandparent include files.
3. Along the path that's specified by each /I compiler option.
4. Along the paths that are specified by the INCLUDE environment variable.

Angle-bracket form
The preprocessor searches for include files in this order:
1. Along the path that's specified by each /I compiler option.
2. When compiling occurs on the command line, along the paths that are specified by the INCLUDE
environment variable.


Including a file using <> will tell the compiler to look for those files in environment-defined inclusion folders. Those folders can be standard system folders, or defined by your Makefile if you use one, etc. Using "", the compiler will look for inclusion files only in the source file's path.
So you can use "" and use absolute paths or the path which is relative to the source file you try to include in, or you can use <> after defining your inclusion folders, and just specify the name of the header file to include.
IMHO, the second option is better, especially if you use a lot of headers, or multiple libraries, etc...

To define inclusion folders at compilation time : gcc -I ... (man gcc!)

0

精彩评论

暂无评论...
验证码 换一张
取 消