For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > what's the different between #incldue " xxx" and #include <xxx >









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author what's the different between #incldue " xxx" and #include <xxx >
boki

2005-06-07, 3:58 am

Hi All,
When we include a header file, we will use

#include "xxx "

or

#include "xxx"

What's the difference?

Best regards,
Boki.




Carl Daniel [VC++ MVP]

2005-06-07, 3:58 am

boki wrote:
> Hi All,
> When we include a header file, we will use
>
> #include "xxx "
>
> or
>
> #include "xxx"
>
> What's the difference?


#include "file.h" will locate file.h in the same directory as the including
file, while <file.h> won't.

-cd


William DePalo [MVP VC++]

2005-06-07, 3:58 am

"boki" <bokiteam@ms21.hinet.net> wrote in message
news:edpw9gxaFHA.3040@TK2MSFTNGP14.phx.gbl...
> What's the difference?


Take a look at this link:

http://msdn.microsoft.com/library/d...e_directive.asp

It includes this explanation:

<quote>
Quoted form This form instructs the preprocessor to look for include files
in the same directory of the file that contains the #include statement, and
then in the directories of any files that include (#include) that file. The
preprocessor then searches along the path specified by the /I compiler
option, then along paths specified by the INCLUDE environment variable.


Angle-bracket form This form instructs the preprocessor to search for
include files first along the path specified by the /I compiler option,
then, when compiling from the command line, along the path specified by the
INCLUDE environment variable.
<quote>

If there is something in the explanation that you don't understand you can
post again.

Regards,
Will




Doug Harrison [MVP]

2005-06-07, 3:58 am

On Tue, 7 Jun 2005 12:20:26 +0800, boki wrote:

> Hi All,
> When we include a header file, we will use
>
> #include "xxx "
>
> or
>
> #include "xxx"
>
> What's the difference?


It's implementation-defined. VC++ implements very sensible rules that make
me think of namespaces. You can find them here:

The #include Directive
http://msdn.microsoft.com/library/d...htm/prepr_8.asp

I use the quoted form for files in my project, and more generally, whenever
I #include a file I can locate relative to the file that contains the
#include. I use the angle bracket form for system headers, and more
generally, headers that are located by consulting the INCLUDE environment
variable or the corresponding IDE setting.

Tip: If you're writing a library, structure your public headers like this:

LibRoot
LibName
Header files, subdirs, etc.

Your users then enter "LibRoot" into INCLUDE, and they #include your
library headers like this:

#include <LibName/header1.h>
#include <LibName/subdir/header2.h>

If LibName is sufficiently unique, then multiple libraries from different
authors can coexist in the same LibRoot directory. Alternatively, you can
make LibRoot unique and use it as your own personal library area, but then
you should put the parent directory of LibRoot into INCLUDE and refer to
headers like this:

#include <LibRoot/LibName/header1.h>

If you don't have a unique path to your headers, you may find yourself in a
situation where order of INCLUDE entries matters, and that's to be avoided.

--
Doug Harrison
Microsoft MVP - Visual C++
Sergei

2005-06-07, 9:02 am

"Roland Frank" <roland.frank.no-spam@gmx.info> wrote:

> Hi boki,
>
> although the previous posters quite correctly explained
> what difference it makes with VC++ to use the "" or <> form
> of the #include directive, they completely fail to explain
> WHY there are two different forms in the first place.
> This all originates in the C++ standard.
> The "" version dates all back to the days C was invented
> and means in short "include the given files here" using an
> implementation defined search sequence for the file name
> between the "".


I remember the <> version was there when I programmed in C

> The <> version is quite different. Avoiding the legalese of
> the standard it means that the char sequence between the
> brackets does NOT necessarily denote a file name. It says
> that it "uniquely identifies a header" in a implementation
> defined way. This could for example mean that the identifier
> between <> is an index into a database and the header fetched
> from there.
> I think the rationale behind this was, that on some operation
> system it might be illegal to use filenames without extensions
> or that there might be a restriction in the length of filenames.
> For example #include <exception> might be an illegal filename
> (it is on DOS).


It is a legal DOS filename.

Sergei

> The creators of the standard wanted to make shure
> that a program that includes the headers as defined in the standard
> to correctly compile on all platforms without changes. So, a DOS
> compiler could lookup the <exception> in the example in a file,
> find the filename "exceptio.hhh" and then use this filename instead.
>
> VC++ implementation of the <> is completely conforming to the
> standard but I wouldn't use the <> form for anything else but
> the headers of the standard library, as you might be in for a
> big surprise if you switch compilers and use the <> as simply
> a means for including files with a different search sequence.
>
> Regards,
> Roland

Sergei

2005-06-07, 9:02 am

"Sergei" <sergei@nospam.summertime.mtu-net.ru> wrote:
> "Roland Frank" <roland.frank.no-spam@gmx.info> wrote:
>
>
> I remember the <> version was there when I programmed in C
>
>
> It is a legal DOS filename.


Pardon, I see now that it's 9 char long.

Sergei
[color=darkred]
> Sergei
>
Doug Harrison [MVP]

2005-06-07, 4:05 pm

On Tue, 07 Jun 2005 09:11:55 +0200, Roland Frank wrote:

> Hi boki,
>
> although the previous posters quite correctly explained
> what difference it makes with VC++ to use the "" or <> form
> of the #include directive, they completely fail to explain
> WHY there are two different forms in the first place.
> This all originates in the C++ standard.
> The "" version dates all back to the days C was invented
> and means in short "include the given files here" using an
> implementation defined search sequence for the file name
> between the "".
> The <> version is quite different. Avoiding the legalese of
> the standard it means that the char sequence between the
> brackets does NOT necessarily denote a file name. It says
> that it "uniquely identifies a header" in a implementation
> defined way. This could for example mean that the identifier
> between <> is an index into a database and the header fetched
> from there.


The same thing applies to the "" form; compilers fall back on the <> form
if the "" form fails or simply isn't supported.

> I think the rationale behind this was, that on some operation
> system it might be illegal to use filenames without extensions
> or that there might be a restriction in the length of filenames.
> For example #include <exception> might be an illegal filename
> (it is on DOS). The creators of the standard wanted to make shure
> that a program that includes the headers as defined in the standard
> to correctly compile on all platforms without changes. So, a DOS
> compiler could lookup the <exception> in the example in a file,
> find the filename "exceptio.hhh" and then use this filename instead.


Correct. The compiler can map names in #include directives directly to
physical filenames, indirectly by performing some transformation on the
filename (e.g. translating path separators), to arrays of text representing
the header contents stored in the compiler itself, etc.

> VC++ implementation of the <> is completely conforming to the
> standard but I wouldn't use the <> form for anything else but
> the headers of the standard library, as you might be in for a
> big surprise if you switch compilers and use the <> as simply
> a means for including files with a different search sequence.


You might also be in for a big surprise if you use the "" form for third
party library headers that don't use sufficiently unique names. That's also
true for the <> form, but at least you don't have to worry about choosing
globally unique names for the headers you create yourself. (Macro guards
are another story, of course!) Again, this is all implementation-defined,
but the implementation I've seen most commonly is what I described in my
earlier message, with "" to refer to files located relative to the file
making the #include, and <> for everything else. It's the most sensible
approach to take; for example, gcc does it this way, too:

http://www.ugcs.caltech.edu/info/gcc/cpp_2.html#SEC8

As does Sun:

http://docs.sun.com/source/819-0494...l#pgfId-1004269

To minimize the danger of conflicting filenames, apply the approach I
described in my earlier message.

--
Doug Harrison
Microsoft MVP - Visual C++
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com