Basic .H/.CPP Location Question

Discussion in 'C++' started by NvrBst, Nov 29, 2008.

  1. NvrBst

    NvrBst Guest

    This seems like a very basic question but I can't seem to google the
    answer. Should the .H and .CPP always be in the same directory? Or
    is it safe to put all my ".h" file in a "inc" folder, and all my .cpp
    files in a "src" folder?

    MSDN says (#include"") "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.". How is the 2nd part of the statment
    possible? For example

    MyProj1/inc/*.h
    MyProj1/src/*.cpp

    MyProj2/inc/*.h
    MyProj2/inc/*.cpp

    Say a file in MyProj2 wants to include something in the "MyProj1/inc"
    folder, it'd go "include <MyProj1File.h>" and then add -I"path/
    MyProj1/inc" to the command line, would I also have to add the source
    directory -I"path/MyProj2/src"?

    This is just a cosmetic thing, I'm a little new to C++ and wanted to
    know if I'd be laughed out of the room by having seperate "inc" and
    "src" folders, and also how a header file finds it's source file (aka
    just magic, the -I flag, or something else).
     
    NvrBst, Nov 29, 2008
    #1
    1. Advertising

  2. NvrBst

    LR Guest

    NvrBst wrote:
    > This seems like a very basic question but I can't seem to google the
    > answer. Should the .H and .CPP always be in the same directory? Or
    > is it safe to put all my ".h" file in a "inc" folder, and all my .cpp
    > files in a "src" folder?


    You can use separate directories. I know some who do. I think it's a
    little unusual.

    >
    > MSDN says (#include"") "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.".


    I don't exactly follow your question and you might be better off in a ng
    specific to your compiler, but, most compilers have flags or settings
    for additional include directories that help to specify the search path.


    >
    > This is just a cosmetic thing, I'm a little new to C++ and wanted to
    > know if I'd be laughed out of the room by having seperate "inc" and
    > "src" folders,


    No, I suspect you might get asked why you want to do it that way. BTW,
    why do you want to do things that way? Also, there can be issues with
    projects that use multiple libs and have .h files with the same names.
    Confusing at least. Sort of a namespace type of thing. People have
    various strategies for dealing with those.

    Another issue is if you're using an IDE, which may have, so to speak,
    it's own ideas, about what files should go in which directories. It can
    be pointlessly painful to fight this.


    LR
     
    LR, Nov 29, 2008
    #2
    1. Advertising

  3. NvrBst

    James Kanze Guest

    On Nov 29, 1:03 am, NvrBst <> wrote:
    > This seems like a very basic question but I can't seem to
    > google the answer.  Should the .H and .CPP always be in the
    > same directory?  Or is it safe to put all my ".h" file in a
    > "inc" folder, and all my .cpp files in a "src" folder?


    There's no real rule. Different projects organize things in
    different ways.

    > MSDN says (#include"") "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.".
    > How is the 2nd part of the statment possible?


    In what way shouldn't it be possible? (Note that not all
    compilers support that second part.)

    > For example


    > MyProj1/inc/*.h
    > MyProj1/src/*.cpp
    >
    > MyProj2/inc/*.h
    > MyProj2/inc/*.cpp


    > Say a file in MyProj2 wants to include something in the "MyProj1/inc"
    > folder, it'd go "include <MyProj1File.h>" and then add   -I"path/
    > MyProj1/inc"  to the command line, would I also have to add the source
    > directory   -I"path/MyProj2/src"?


    First, you'd probably want to use ``#include "MyProj1File.h"'';
    the <...> form is for "system" headers. And why would you have
    to add the source directory to the list? You're not including
    anything from it. (Also, I'm very dubious about including a
    header from another project. Normally, you would export the
    project at some point when it is stable, and include the header
    from where ever you exported it to.)

    > This is just a cosmetic thing, I'm a little new to C++ and
    > wanted to know if I'd be laughed out of the room by having
    > seperate "inc" and "src" folders,


    Certainly not. It seems to be a common organization for
    freeware.

    > and also how a header file finds it's source file (aka just
    > magic, the -I flag, or something else).


    The header file doesn't find its source file. It has no need of
    the source file. The linker will need to find the corresponding
    library, of course, so you'll have to provide the appropriate
    flags there.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 29, 2008
    #3
  4. NvrBst

    NvrBst Guest

    On Nov 29, 2:26 am, James Kanze <> wrote:
    > On Nov 29, 1:03 am, NvrBst <> wrote:
    >
    > > This seems like a very basic question but I can't seem to
    > > google the answer.  Should the .H and .CPP always be in the
    > > same directory?  Or is it safe to put all my ".h" file in a
    > > "inc" folder, and all my .cpp files in a "src" folder?

    >
    > There's no real rule.  Different projects organize things in
    > different ways.
    >
    > > MSDN says (#include"") "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.".
    > > How is the 2nd part of the statment possible?

    >
    > In what way shouldn't it be possible?  (Note that not all
    > compilers support that second part.)
    >
    > > For example
    > > MyProj1/inc/*.h
    > > MyProj1/src/*.cpp

    >
    > > MyProj2/inc/*.h
    > > MyProj2/inc/*.cpp
    > > Say a file in MyProj2 wants to include something in the "MyProj1/inc"
    > > folder, it'd go "include <MyProj1File.h>" and then add   -I"path/
    > > MyProj1/inc"  to the command line, would I also have to add the source
    > > directory   -I"path/MyProj2/src"?

    >
    > First, you'd probably want to use ``#include "MyProj1File.h"'';
    > the <...> form is for "system" headers.  And why would you have
    > to add the source directory to the list?  You're not including
    > anything from it.  (Also, I'm very dubious about including a
    > header from another project.  Normally, you would export the
    > project at some point when it is stable, and include the header
    > from where ever you exported it to.)
    >
    > > This is just a cosmetic thing, I'm a little new to C++ and
    > > wanted to know if I'd be laughed out of the room by having
    > > seperate "inc" and "src" folders,

    >
    > Certainly not.  It seems to be a common organization for
    > freeware.
    >
    > > and also how a header file finds it's source file (aka just
    > > magic, the -I flag, or something else).

    >
    > The header file doesn't find its source file.  It has no need of
    > the source file.  The linker will need to find the corresponding
    > library, of course, so you'll have to provide the appropriate
    > flags there.
    >
    > --
    > James Kanze (GABI Software)             email:
    > Conseils en informatique orientée objet/
    >                    Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Thanks, I understand it better now -headers don't need their source
    files-; that was the part that was tripping me up. :) So I just have
    to find the "-I" equavalent(maybe same) flag for the linker (pointing
    to the .o files). Thanks again.
     
    NvrBst, Nov 29, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DrUg13
    Replies:
    1
    Views:
    497
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    428
    Greg Comeau
    Nov 15, 2004
  3. Vinu
    Replies:
    9
    Views:
    683
  4. www.hitechskill.com
    Replies:
    0
    Views:
    1,392
    www.hitechskill.com
    Apr 9, 2006
  5. key9
    Replies:
    7
    Views:
    388
    Pete Becker
    Sep 13, 2006
Loading...

Share This Page