Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Basic .H/.CPP Location Question

Reply
Thread Tools

Basic .H/.CPP Location Question

 
 
NvrBst
Guest
Posts: n/a
 
      11-29-2008
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).
 
Reply With Quote
 
 
 
 
LR
Guest
Posts: n/a
 
      11-29-2008
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-29-2008
On Nov 29, 1:03*am, NvrBst <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
NvrBst
Guest
Posts: n/a
 
      11-29-2008
On Nov 29, 2:26*am, James Kanze <(E-Mail Removed)> wrote:
> On Nov 29, 1:03*am, NvrBst <(E-Mail Removed)> 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:(E-Mail Removed)
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
NAT location and VPN termination location K.J. 44 Cisco 0 09-12-2006 02:17 PM
after an image is moved to a new location, how to delete the image at the old location? zxo102 Python 0 08-01-2006 04:58 PM
How to trick a page into running at a location different from it'sphysical location? Luke Dalessandro ASP .Net 0 01-15-2006 05:59 AM
difference between location.href and window.location.href? saiho.yuen Javascript 3 09-14-2004 06:51 PM



Advertisments