[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Basic .H/.CPP Location Question

NvrBst

11/29/2008 12:04:00 AM

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).
3 Answers

LR

11/29/2008 2:30:00 AM

0

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

James Kanze

11/29/2008 10:27:00 AM

0

On Nov 29, 1:03 am, NvrBst <nvr...@gmail.com> 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:james.kanze@gmail.com
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

NvrBst

11/29/2008 6:09:00 PM

0

On Nov 29, 2:26 am, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 29, 1:03 am, NvrBst <nvr...@gmail.com> 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:james.ka...@gmail.com
> 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.