Using Projects with Visual C++
This document was written by Chris Nevison
Introduction
This information applies to Microsoft Visual C++ versions 4.0, 4.2,
and 5.0.
When using VC++ you can simply write C++ code in a text file (*.cpp)
and ask the system to build (compile and link) the program that you have
written. The system will ask if you want it to create a workspace (project)
for you. If you answer yes, the project is created and the program is compiled
and linked and, if it has no errors, ready to run. However, it is useful
to work with the workspace/project explicitly as soon as programs need
code, such as a class, which is defined in separate files.
Creating Projects
- To create a new project, select File, New, Projects, Win32 Console
Application (for VC++5.0) or File, New, Project Workspace, Console
Application (for VC++4.0/4.2)
- Specify the directory where the project should be stored and the name
of the project.
- If you need to write code for the new project, simply select File,
New, Files, C++ Source(or Header) File (for VC++5.0) or File, New,
Text File (for VC++4.0/4.2)
- For any source code file (*.cpp) used in the project, it must be included
in the project. The interface files (*.h) are not included in the project
and the code files for template classes or functions are not included in
the project. To add a file to a project, select Project, Add to Project,
Files (for VC++5.0) or Insert, Files into Project (for VC++4.0/4.2).
- In order to compile and link the project, select Build, Build
(VC++5.0/4.0/4.2). This will recompile only those files which havebeen
changed since the last Build. If you want to force all files to be recompiled
(often a good idea if you get errors you cannot figure out), select Rebuild.
- To run the program, Select Build, Execute (VC++5.0/4.0/4.2).
Creating a Library
It is very helpful to have students use different tools which you provide,
such as apstring or dice, early in the course before you want to teach
them how to deal with projects based on multiple code files. You can do
this if you provide a library containing your tools which is automatically
accessed. This can be done in VC++ with a small "cheat." You
can build a library as follows:
- It is a good idea to collect all the code files that you plan to include
in the library in one directory called source. Remember that template code
(such as apvector.cpp) does not go into the library.
- Create a new project by selecting File, New, Projects, Win32 Static
Library (VC++5.0) or File, New, Project Workspace, Static Library
(VC++4.0/4.2). Provide the location and name for your library (for example
aplib).
- Add the *.cpp files that you want in your library to your project.
To add a file to a project, select Project, Add to Project, Files
(for VC++5.0) or Insert, Files into Project (for VC++4.0/4.2).
- Compile and create the library by selecting Build, Build (VC++5.0/4.0/4.2).
You now have your library created under the name you designated before.
- Exit from the developer environment and copy your library, aplib.lib,
to the directory where you want the library stored. Note, the library will
probably be found in the Debug subdirectory in the directory created for
you project.
- Rename your library ole32.lib. This is the name of a system library
which you will not need for introductory level programming (it is used
for "object linking and embedding").
- In order that your projects find the interface files (*.h) and the
library, you need to set the correct directory paths.
- Start the VC++ environment.
- Select Tools, Options, Directories
- Under the "Show Directories for" window select Include
files
- At the bottom of the list type in the path to the directory which contains
your include files (*.h)
- Click and drag that path to the top of the list.
- Under the "Show Directories for" window select Library files
and follow the same steps to place the path to the directory containing
your library at the top of the list
- Under the "Show Directories for" window select Sourcefiles
and follow the same steps to place the path to the directory containing
your source code (*.cpp) at the top of the list
- These directory designations will remain for all projects created (on
this particular system) until they are changed.
- The library which you created and named "ole32.lib" will
override the system library of the same name only if your library is listed
first under the directories option.
After your library has been created and the directory paths have been
set, you can write programs which use either your library classes and functions
or system library classes and functions by simply including the appropriate
interface (*.h) file at the top of the code file where that library code
is to be used. You can use the <> deleimiters with the include, as
for example: #include <apstring.h>