Include header files in quotes or less-than greater-than signs?

  • Thread starter Dwight Army of Champions
  • Start date
D

Dwight Army of Champions

What is the difference to including .h files in quotes and in less-
than greater-than signs? Also sometimes I see the ".h" portion
included and sometimes I don't (example: #include <vector>). What's
the difference?
 
M

Marcel Müller

Dwight said:
What is the difference to including .h files in quotes and in less-
than greater-than signs? Also sometimes I see the ".h" portion
included and sometimes I don't (example: #include <vector>). What's
the difference?

- <> searches the include directories
- "" searches the current directory
- Some compilers ignore that.


Marcel
 
R

Robert Fendt

- <> searches the include directories
- "" searches the current directory
- Some compilers ignore that.

If I recall correctly "" falls back to <> if it does not find
anything (at least I think I read something like that in the
standard somewhere).

The traditional include syntax distinction is part of C++'s C
heritage, and apart from that it can help improve readability as
well.

Regards,
Robert
 
J

James Kanze

What is the difference to including .h files in quotes and in
less- than greater-than signs? Also sometimes I see the ".h"
portion included and sometimes I don't (example: #include
<vector>). What's the difference?

In all cases, it's implementation defined. For the first,
almost all compilers will first look for the included file in
the directory which contains the file doing the include, then
continue as if you'd used <...>, e.i. looking in the directories
given with the -I or /I options, then in a number of fixed
locations. For the second, how the compiler maps the string
given in the include directive is implementation defined, but I
don't know of a single implementation today which doesn't treat
it literally: <vector> and <vector.h> designate two different
files.
 
J

John H.

Also sometimes I see the ".h" portion
included and sometimes I don't (example: #include <vector>). What's
the difference?

The difference is just the naming convention. Another one you might
find is ".hpp". Under the hood they are all the same thing, headers.
Most likely they are the name of header files sitting on your machine
somewhere.

The C standard library has a lot of header files ending in .h, e.g.
<stdlib.h>. When early C++ compilers incorporated the C standard
library, the easiest thing to do was to keep the same naming
conventions. So in a C program you would #include <stdlib.h>, and in
a C++ program you would #include <stdlib.h> to do the same thing.
Some features that were not part of C were also added to C++, such as
vectors, and early compilers might have used <vector.h> or <vector>.

When C++ was standardized, they decided to put the functionality of
<stdlib.h> into what C++ called <cstdlib>, similarly <assert.h> became
<cassert>, etc. The library features that were new with C++ were also
standardized, with header names that lacked the .h (e.g. <vector>,
<fstream>, etc). In addition, the functionality of the standard
library via these headers was mostly put into the namespace std.
Finally, support for the old C style headers (<stdlib.h>) was kept.

As it happens, adoption of the standardized header naming and
namespace placement by different compilers happened at different times
and sometimes incrementally between releases of a compiler. Some
compilers just know about the C name <stdlib.h>, some will offer both
C and C++ name (stdlib.h or cstdlib). In addition, some compilers put
the contents of into the std namespace, where some did not. In
recognition of this problem, in C++0x, they are specifying that it is
up the implementation if they put the function in just the std
namespace or both the std and global namespace.

So when reading code that uses functionality from the C library, you
might see either the C style name (e.g. <math.h>) or the C++ style
name (<cmath>). In addition, it may or may not be using the std
namespace. When writing code, it is probably best to use the C++
style name and assume that things are in the std namespace.

Another little thing, C and C++ have distinct string concepts. The C
style name for the C string concept is <string.h>. The C++ style name
for the C string is <cstring>. The C++ name for the C++ string is
<string> (there is no C style name for the C++ string).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top