When to use #include <> and #include " "

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tuckers

    When to use #include <> and #include " "

    My question is, if I have created my own library which lives in its own
    install directory, to refer to its header file is it better to use

    #include "MyLibrary. h"

    or

    #include <MyLibrary.h>

    Assume that this library will be used by other groups. I think the answer
    should be use the "<>" notation but I would be interested why.

    cheers

    Tuckers


  • ben

    #2
    Re: When to use #include &lt;&gt; and #include &quot; &quot;

    <> is reserved for the standard library and some system libraries. so you
    may use ""

    ben

    "Tuckers" <matt@work.co m> wrote in message news:d6cpp0$pb$ 1@rdel.co.uk...[color=blue]
    > My question is, if I have created my own library which lives in its own
    > install directory, to refer to its header file is it better to use
    >
    > #include "MyLibrary. h"
    >
    > or
    >
    > #include <MyLibrary.h>
    >
    > Assume that this library will be used by other groups. I think the answer
    > should be use the "<>" notation but I would be interested why.
    >
    > cheers
    >
    > Tuckers
    >
    >[/color]


    Comment

    • Ron Natalie

      #3
      Re: When to use #include &lt;&gt; and #include &quot; &quot;

      ben wrote:[color=blue]
      > <> is reserved for the standard library and some system libraries. so you
      > may use ""
      >[/color]

      <> assures that if you give it the name of a standard header, you get
      that for sure. Otherwise the behavior of #include is implementation
      defined as to what the interpretation of the quoted string is (and
      which set of delimeters you used).

      Comment

      • Rapscallion

        #4
        Re: When to use #include &lt;&gt; and #include &quot; &quot;

        ben wrote:[color=blue]
        > <> is reserved for the standard library and some system libraries. so[/color]
        you[color=blue]
        > may use ""[/color]

        Nonsense. The path in which the header is looked up is different,
        nothing else (see a good C++ book for details).

        R.C.

        Comment

        • Jaspreet

          #5
          Re: When to use #include &lt;&gt; and #include &quot; &quot;

          > #include "MyLibrary. h"
          Generally reserved for headers which are a part of the system package
          [color=blue]
          > #include <MyLibrary.h>[/color]
          Usually for user defined headers.

          I am not sure but the compiler first starts looking in /usr/include or
          the INCLUDE path for headers within <> and then searches in the current
          directory if it does not find the header there.

          For headers within "", the compiler first starts looking in the current
          directory and then moves to the INCLUDE directory path.

          Let me know if I am wrong.

          Comment

          • Rapscallion

            #6
            Re: When to use #include &lt;&gt; and #include &quot; &quot;

            Ron Natalie wrote:[color=blue]
            > ben wrote:[color=green]
            > > <> is reserved for the standard library and some system libraries.[/color][/color]
            so you[color=blue][color=green]
            > > may use ""[/color]
            >
            > <> assures that if you give it the name of a standard header, you get
            > that for sure.[/color]

            Where did you get this from???
            [color=blue]
            > Otherwise the behavior of #include is implementation
            > defined as to what the interpretation of the quoted string is (and
            > which set of delimeters you used).[/color]

            incomprehensibl e.

            Comment

            • Howard

              #7
              Re: When to use #include &lt;&gt; and #include &quot; &quot;


              "Jaspreet" <jsingh.oberoi@ gmail.com> wrote in message
              news:1116338537 .535833.275660@ f14g2000cwb.goo glegroups.com.. .[color=blue][color=green]
              >> #include "MyLibrary. h"[/color]
              > Generally reserved for headers which are a part of the system package
              >[color=green]
              >> #include <MyLibrary.h>[/color]
              > Usually for user defined headers.
              >[/color]

              Those are backwards.
              [color=blue]
              > I am not sure but the compiler first starts looking in /usr/include or
              > the INCLUDE path for headers within <> and then searches in the current
              > directory if it does not find the header there.
              >
              > For headers within "", the compiler first starts looking in the current
              > directory and then moves to the INCLUDE directory path.
              >
              > Let me know if I am wrong.
              >[/color]

              You are wrong. :-)

              The <> notation directs the compiler to look in whatever it defines as the
              "system directories" (which is dependent on the OS, and possibly on settings
              the user can make to the OS, such as path variables). (Note that there IS
              NO /usr/include on a Windows system.) In any case, provided that the
              compiler has been "properly" installed (i.e., its libraries haven't been
              manually fooled with), this notation specifies the "system" files, meaning
              all the standard headers, plus any that have been installed in that same
              path, plus any that might be included by the user having added to the system
              path information.

              The "" notation is generally used for "user" files, and can be just about
              anything, depending on the compiler implementation. Generally, they're the
              files that are somehow specified as part of the "project", or for which the
              project has been told where to look for its source files (via "project
              settings"), or files in the project's home directory.

              So... use <> for the standard headers. Use "" for your own files. For
              third-party files, it will depend on where they've been installed and how
              your system+compiler +project are set up currently. You could try using <>
              and see if that works. If not, use "". If it still doesn't work, look up
              how to specify where to find user files in your compiler's documentation.

              -Howard



              Comment

              • Alf P. Steinbach

                #8
                Re: When to use #include &lt;&gt; and #include &quot; &quot;

                * Tuckers:[color=blue]
                > My question is, if I have created my own library which lives in its own
                > install directory, to refer to its header file is it better to use
                >
                > #include "MyLibrary. h"
                >
                > or
                >
                > #include <MyLibrary.h>
                >
                > Assume that this library will be used by other groups. I think the answer
                > should be use the "<>" notation but I would be interested why.[/color]

                The standard only specifies that "" may do some _additional_ searching.

                In practice that additional searching includes the directory of the file
                containing the #include directive.

                Just for completeness: it's also valid to use a macro for the filename.

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • Krishanu Debnath

                  #9
                  Re: When to use #include &lt;&gt; and #include &quot; &quot;

                  Rapscallion wrote:[color=blue]
                  > Ron Natalie wrote:
                  >[color=green]
                  >>ben wrote:
                  >>[color=darkred]
                  >>><> is reserved for the standard library and some system libraries.[/color][/color]
                  >
                  > so you
                  >[color=green][color=darkred]
                  >>>may use ""[/color]
                  >>
                  >><> assures that if you give it the name of a standard header, you get
                  >>that for sure.[/color]
                  >
                  >
                  > Where did you get this from???[/color]

                  From Standard, Sec 16.2. It says ...

                  -2- A preprocessing directive of the form

                  # include <h-char-sequence> new-line

                  searches a sequence of implementation-defined places for a header
                  identified uniquely by the specified sequence between the < and >
                  delimiters.

                  So if your compiler implementation cannot find a standard header file,
                  included in the above manner, it is very likely that you are using a
                  broken compiler.
                  [color=blue]
                  >
                  >[color=green]
                  >>Otherwise the behavior of #include is implementation
                  >>defined as to what the interpretation of the quoted string is (and
                  >>which set of delimeters you used).[/color]
                  >
                  >
                  > incomprehensibl e.
                  >[/color]

                  What?

                  Krishanu

                  Comment

                  • jdibling@gmail.com

                    #10
                    Re: When to use #include &lt;&gt; and #include &quot; &quot;

                    Be careful when you #include within a header file.

                    Suppose you have a header file foo.h, which reads like this:

                    [foo.h]

                    #include <bar.h>

                    class FooFighter
                    {
                    static Bar* CreateFoo(); // Bar is declared in bar.h
                    };

                    ------------------------------

                    foo.h and bar.h are in the directory c:\foostuff. Let's say that
                    c:\foostuff is not in the system directory.

                    Your client code is like this:

                    [main.cpp]

                    #include "foo.h"

                    int main()
                    {
                    return 0;
                    }
                    -------------------

                    ....and main.cpp is in c:\mygizmo.

                    If you compile this code, you'll get an error becasue - even though
                    foo.h and bar.h are in the same directory - the compiler won't be able
                    to find bar.h becasue it was #include'd using the angle brackets.

                    Comment

                    • davidrubin@warpmail.net

                      #11
                      Re: When to use #include &lt;&gt; and #include &quot; &quot;

                      Using quotes is artificial considering that you can specified -I rules
                      (or whatever your compiler supports) to search in whatever directories
                      you want for header files. Hence you can use these rules, and include
                      everything using <>. /david

                      Comment

                      • John Dibling

                        #12
                        Re: When to use #include &lt;&gt; and #include &quot; &quot;

                        What do you mean by "artificial ?"

                        As far as I can tell, you have devised a way to defeat the intention of
                        the standard - e.g., the intention to allow #include "" and #include <>
                        to behave differently.

                        Is this correct, and if so, what benefit do you gain?

                        </dib>

                        Comment

                        • davidrubin@warpmail.net

                          #13
                          Re: When to use #include &lt;&gt; and #include &quot; &quot;

                          The only benefit of having I can see in having both "" and <> is to
                          replace an existing header file in the standard include path with a
                          local version. You can do this just as easily with -I rules. Otherwise,
                          you are just making an unnecessary distinction between what is "local"
                          and what is installed in the system include path. For example, if you
                          decide to move your local llibrary include files into the system path
                          (e.g., /usr/local/include), the logical separation implied by including
                          with quotations is no longer accurate.

                          Furthermore, it is much easier to perform source-code analysis and to
                          generate code programatically if you stick to one include template and
                          change only the build meta-data.

                          Comment

                          • Rapscallion

                            #14
                            Re: When to use #include &lt;&gt; and #include &quot; &quot;

                            Krishanu Debnath wrote:[color=blue]
                            > From Standard, Sec 16.2. It says ...
                            >
                            > -2- A preprocessing directive of the form
                            >
                            > # include <h-char-sequence> new-line
                            >
                            > searches a sequence of implementation-defined places for a header
                            > identified uniquely by the specified sequence between the < and >
                            > delimiters.[/color]

                            You left out importent parts.
                            # include <h-char-sequence>
                            "How the places are specified or the header identified is
                            implementation-defined."

                            #include "q-char-sequence"
                            "The named source file is searched for in an implementation-defined
                            manner."

                            In both cases almost nothing is defined by the C++ Standard. Most
                            things are left "implementa tion-defined".
                            [color=blue]
                            > So if your compiler implementation cannot find a standard header[/color]
                            file,[color=blue]
                            > included in the above manner, it is very likely that you are using a
                            > broken compiler.[/color]

                            Not at all. At least not according to the C++ Standard.

                            R.C.

                            Comment

                            • Ron Natalie

                              #15
                              Re: When to use #include &lt;&gt; and #include &quot; &quot;

                              Howard wrote:[color=blue]
                              > "Jaspreet" <jsingh.oberoi@ gmail.com> wrote in message[/color]
                              [color=blue]
                              >
                              > You are wrong. :-)
                              >
                              > The <> notation directs the compiler to look in whatever it defines as the
                              > "system directories"[/color]

                              Actually, you're also wrong. It's entirely implementation defined. The
                              only thing know for a fact is that "" will also search do the <>
                              as a last resort.

                              Comment

                              Working...