Google Answers Logo
View Question
 
Q: visual c++ multifile program organization ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: visual c++ multifile program organization
Category: Computers > Programming
Asked by: adman1973-ga
List Price: $25.00
Posted: 14 Jul 2006 09:51 PDT
Expires: 13 Aug 2006 09:51 PDT
Question ID: 746325
All my previous programs have had the classes and main() in the same
.cpp file.  For the first time, I have separated the classes into
header (.h) and source (.cpp) files.  My general question is: if I
want to have two separate application programs (i.e. 2 separate
main()?s) that will use these classes, what is the standard way of
organizing the various files?  Should I (1) add a new workspace to the
original project folder, thereby, having two workspaces and their
respective files in the same project folder? (2) add a new project
folder to the original folder? (3) put the class files in a separate
folder, then have two separate project folders for the two different
application programs?

I would just like to know what the standard practice is.

(I use MS VC 6.0.)
Answer  
Subject: Re: visual c++ multifile program organization
Answered By: efn-ga on 14 Jul 2006 20:49 PDT
Rated:5 out of 5 stars
 
Hi adman1973-ga,

There are a few different levels of organization to consider.  I will
take them one at a time.

The first level is how you organize your source files into the
components you compile and link, which correspond to the projects you
define in the Visual Studio development environment.  You have two
choices here:  you can have two projects that build executable
programs that happen to have some source files in common, or you can
have three projects.  In the three-project design, you would move the
common files to a project that builds a library.  Then you would have
two other projects that build the two different executable programs. 
These projects would link in the library from the first project, but
not contain the source files that are used to build the library.

Of these choices, the two-project design will be easier to set up, and
the three-project design would be standard professional practice. 
With big enough projects, the compiling time you can save by reusing
object code rather than source code can be significant, and it is
easier to manage aggregations of object code in libraries than
individual object files.  If you are dealing with just two small
projects, these considerations may not apply to your situation.

In Visual Studio, a workspace is just a container for related
projects.  I don't think there is any standard practice other than to
put projects that you want to work on together in the same workspace. 
Putting projects in the same or different workspaces has no effect on
how they build; it only affects whether you see them at the same time
when you look at a workspace or you have to change workspaces to
change from one to another.  So if you want to work on these two
programs together, it would make sense to put them into one workspace,
and if you want to focus on only one at a time, you might want to put
them in separate workspaces.  This is just a question of your working
preference.

Independent of the organization of your files into projects and the
organization of your projects into workspaces, you must decide how to
organize the files in folders in the file system on your disk drive. 
Standard practice would be to have one folder per project, with that
project's files, and only that project's files, in that folder.  This
would work with the three-project design, where you would have three
separate folders for the library and the two programs that use the
library.  It would not work so well with the two-project design.  With
the two-project design, I would advise you to keep all the source
files in the same folder, rather than follow standard practice and
have a folder per project.  If you had a folder per project, you would
have to duplicate the common source files in each folder, and having
two copies of the same file in different places is very undesirable
for maintenance.

If a workspace contains only one project, it's standard practice to
store the workspace file in the same folder as the project's files. 
If a workspace contains multiple projects, you can store it anywhere
where you can conveniently find it.  One straightforward design would
be to have a higher-level folder that contains the workspace file and
the folders for the projects that the workspace contains.

It's hard to evaluate the three options you listed, because when you
talk about "project folders," I'm not sure if you are talking about
projects or folders, which are two different things.  I'm also not
sure if you are talking about organizing the files in the file system
or in the Visual Studio project/workspace structure, or both.


I hope this explanation is helpful.  If it is not clear enough or you
need more information, please ask for a clarification and I will
explain further.

--efn
adman1973-ga rated this answer:5 out of 5 stars

Comments  
Subject: Re: visual c++ multifile program organization
From: storm_-ga on 14 Jul 2006 12:09 PDT
 
Hi adman1973-ga, 

As I understand you these classes are some classes-library. There are
several methods of reusing code. The simplest way is the your third
one: put the class files in a separate folder and add these files to
the every project (by the "Add files to the project" menu item in VC++
6.0). Also you can make dynamic (dll) or static library from these
classes and use this library in your projects.

For more information see 

http://en.wikipedia.org/wiki/Code_reuse

and 

http://en.wikipedia.org/wiki/Dynamic_link_library
http://en.wikipedia.org/wiki/Static_library

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy