In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include
statement, as follows?
#include <filename>
#include "filename"
In the C and C++ programming languages, what is the difference between using angle brackets and using quotes in an include
statement, as follows?
#include <filename>
#include "filename"
is used to include standard library files. So the compiler will check in the locations where standard library headers are residing.
will tell the compiler to include user-defined header files. So the compiler will check for these header files in the current folder or
-I
defined folders.In C++, include a file in two ways:
The first one is #include which tells the preprocessor to look for the file in the predefined default location. This location is often an INCLUDE environment variable that denotes the path to include files.
And the second type is #include "filename" which tells the preprocessor to look for the file in the current directory first, then look for it in the predefined locations user have set up.
Thanks for the great answers, esp. Adam Stelmaszczyk and piCookie, and aib.
Like many programmers, I have used the informal convention of using the
"myApp.hpp"
form for application specific files, and the<libHeader.hpp>
form for library and compiler system files, i.e. files specified in/I
and theINCLUDE
environment variable, for years thinking that was the standard.However, the C standard states that the search order is implementation specific, which can make portability complicated. To make matters worse, we use jam, which automagically figures out where the include files are. You can use relative or absolute paths for your include files. i.e.
Older versions of MSVS required double backslashes (\\), but now that's not required. I don't know when it changed. Just use forward slashes for compatibility with 'nix (Windows will accept that).
If you are really worried about it, use
"./myHeader.h"
for an include file in the same directory as the source code (my current, very large project has some duplicate include file names scattered about--really a configuration management problem).Here's the MSDN explanation copied here for your convenience).
The only way to know is to read your implementation's documentation.
In the C standard, section 6.10.2, paragraphs 2 to 4 state:
There exists two ways to write #include statement.These are:
The meaning of each form is
This command would look for the file
mylib.h
in the current directory as well as the specified list of directories as mentioned n the include search path that might have been set up.This command would look for the file
mylib.h
in the specified list of directories only.The include search path is nothing but a list of directories that would be searched for the file being included.Different C compilers let you set the search path in different manners.
In practice, the difference is in the location where the preprocessor searches for the included file.
For
#include <filename>
the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.For
#include "filename"
the preprocessor searches first in the same directory as the file containing the directive, and then follows the search path used for the#include <filename>
form. This method is normally used to include programmer-defined header files.A more complete description is available in the GCC documentation on search paths.