









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Professor: Brinkerhoff; Class: SI Object-Oriented Programming; Subject: Computer Science; University: Weber State University; Term: Spring 2008;
Typology: Study Guides, Projects, Research
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Compiling and Running C++ Programs with Visual Studio 2005....................................... 1 Document Information.................................................................... 1 Contents............................................................................... 1 Checklist............................................................................... 1 Visual Studio / Integrated Development Environment (IDE) Operation.................................. 2 Visual Studio and C++: Step-By-Step......................................................... 2 Start the IDE........................................................................ 2 Create A New, Empty Project........................................................... 2 Make a Console Project................................................................ 2 Configure the Project as Empty.......................................................... 3 Add A New File to the Project........................................................... 4 Compile and Run the Program........................................................... 5 Additional C++ Tasks..................................................................... 5 Add an existing file to an existing project.................................................. 5 Debug vs. Release Executable........................................................... 6 Locating Project Files................................................................. 6 Directory Tree: “Where Are My Files?”....................................................... 9 Command Line Operation.................................................................... 10 Opening a Command Prompt Window................................................... 10 Compile a C++ Source Code File....................................................... 12 Compiling and Linking Multi-File Programs............................................... 12 Makefiles and The make/nmake Command....................................................... 13 Makefile Format........................................................................ 13 How Make Works....................................................................... 13 General Rules.......................................................................... 14 Other Tricks........................................................................... 14
Create a new project ‘ Win32 Console Application (p. 2) ‘ Project location (p. 2) ‘ Project name (p. 2) ‘ Solution name (p. 2) ‘ Make the project empty (p. 3)
Add a new file to the project ‘ Choose a .cpp or .h file as is appropriate (p. 4) ‘ Name the file (p. 4)
Add an existing file to the project (this does not change the physical location of the file) ‘ Add the file to the project (p. 5) ‘ Find and select the file by name (p. 5)
b. Double-click the desktop short cut (if available)
d. Choose type and name file i. Verify that Visual C++ is selected ii. Select “C++ File (.cpp)” or “Header File (.h)” as is appropriate iii. Name the file (note that the studio supplies the extension – .cpp or .h – as needed) iv. Press “Add” v. This opens an editor window into which you may enter a program file
e. Solution directory contains i. The project directory ii. The Debug directory iii. The Release directory iv. And Descriptive project files
f. Open the Debug directory Project directory (i in the above illustration) i. This is where the source code file is located
g. Open the Debug directory i. The executable is named after the project ii. The executable is large because it contains code to support the studio debugger
h. Open the Release directory (only created if the project is built in release mode – see page 5) i. The executable is named after the project ii. This executable is much smaller because it does not contain the debug code
b. This window has the command line version of the compiler in its PATH environment; it also sets INCLUDE and LIB environment variables to locate header files (the target of #include) and libraries respectively c. cd (change directory) to the folder/directory where you are working and proceed
a. Create a desktop shortcut i. Locate (but do not click) the command prompt (see 2.a) ii. Right-click the command prompt iii. Select: Send To º Desktop (create shortcut) b. Right-click on the Commands Prompt icon (just created) c. Appropriately name the shortcut d. Modify the shortcut properties to include the command line configuration command i. Right-click the shortcut ii. Select Properties iii. The Target field currently contains %SystemRoot%\system32\cmd.exe iv. Append the following, beginning with a space and including the quotation marks: /K "C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32" v. Press OK
d. Update the value of the PATH variable i. Place the mouse pointer in the “Variable value” and press the “End” keyboard button ii. type a single semicolon(;) iii. Enter the location of the command line compiler and dynamic libararies: C:\Program Files\Microsoft Visual Studio 8\VC\bin;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE iv. If no errors were made press the OK buttons, one after another, on all three windows; if an error occurred, press Cancel and restart from the previous window e. Create or update the value of the INCLUDE variable i. Search “System variables” for the INCLUDE variable: press “Edit” if it already exists or press “New” if it does not exits ii. Add the following paths: C:\Program Files\Microsoft Visual Studio 8\VC\include;C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include iii. If no errors were made press the OK buttons, one after another, on all three windows; if an error occurred, press Cancel and restart from the previous window f. Create or update the LIB variable i. Search “System variables” for the LIB variable: press “Edit” if it already exists or press “New” if it does not exits ii. Add the following path: C:\Program Files\Microsoft Visual Studio 8\VC\lib iii. If no errors were made press the OK buttons, one after another, on all three windows; if an error occurred, press Cancel and restart from the previous window g. The new PATH, INCLUDE, and LIB environment variables will apply to all newly opened command prompt windows
One of the advantages of working in an integrated development environment (described in the next section) is that the IDE manages the process of compiling and linking multi-file programs. This feature, however, grew out of an older utility named make, which provides the same functionality. The make utility (the Windows version is named nmake) reads a user-created text file that contains a description of how to build a program (i.e., a description of how a set of files should be compiled and liked to create a program) called a makefile. (the make utility and makefiles have more general uses but the discussion here is limited to building programs; furthermore, they may also be used with any language that has a command line version of the compiler, but only C++ programs are illustrated here.)
Makefiles contain a series of rules. Each rule contains at least one line that contains a target and a dependancy. The target is the name of the file that the make utility will create; the dependancy-list is a set of files that must exist and be up to date before the target can be created. Rules also contain zero or more (but typically one) line that specifies the command used to build the target from the dependancy files. It is important to note that each command line must begin with a TAB character as illustrated below:
target : dependancy-list
When the make utility is executed without arguments, it searches the current directory for a makefile. On a Windows platform, the make utility searches for a file name makefile by default. On systems that distinguish between upper and lower case characters (e.g., Unix or Linux), it searches first for a file named Makefile and then for a file named then makefile. Makefiles may be given any legal system name but the name passed to the make utility with the -f or /f command line option. This permits having multiple makefiles in a single directory.
make -f project. or nmake /f project.
It is possible, and quite common, for a file name to appear as a dependency in one rule and as a target in a subsequent rule. In the following example, the files db.obj, comp.obj, io.obj, and tree.obj first appear in the dependency list of the first rule and then again as the target of one of the following rules.
db.exe : db.obj comp.obj io.obj tree.obj cl /o db db.obj comp.obj io.obj tree.obj db.obj : db.cpp db.h cl /c db.cpp comp.obj : comp.cpp db.h cl /c comp.cpp io.obj : io.cpp db.h tree.h cl /c io.cpp tree.obj : tree.cpp db.h tree.h cl /c tree.cpp
When the make utility reads the above makefile, it builds the dependency tree illustrated below. The target of the first rule becomes the root of the tree; the other rules may appear in any order. The make utility works from the leaves toward the root of each sub-tree, invoking the command line version of the Microsoft compiler to create each target, which is the root of a sub-tree. A target or root is recompiled if its timestamp is older than the timestamp of any of its sub-trees.
Assume, for example, that all of the .cpp files are compiled to produce the illustrated object files and further that the object files linked to create the executable. If db.c is modified, the next time the make utility runs, it will find that db.obj is older than db.out, which causes it to recompile db.cpp. Following this, db.exe will older than db.obj, which causes it relink the object files together to recreate db.exe. If tree.h is modified, then io.cpp and tree.cpp are recompiled (both depend on this header file) and then all of the object files are relinked. The make utility only rebuilds files that become out of date when dependant files are modified.
The makefile illustrated above may be simplified by using a general rule (in bold in the following example).
.cpp.obj: cl /c $< db.exe : db.obj comp.obj io.obj tree.obj cl /o db db.obj comp.obj io.obj tree.obj db.obj : db.cpp db.h comp.obj : comp.cpp db.h io.obj : io.cpp db.h tree.h tree.obj : tree.cpp db.h tree.h
The general rule indicates that whenever an object file ( .obj ) is needed and a C++ source file ( .cpp ) is available, that the former can be created from the latter with the cl command. The string $< in the second line of the general rule is a macro or placeholder: it is replaced with the name of the C++ file when the compiler executes.
It is sometimes convenient to have a rule whose target is not reference in the dependency list of the first rule. In this case, the target can be specified when the make utility is executed:
.cpp.obj: cl /c $< db.exe : db.obj comp.obj io.obj tree.obj cl /o db db.obj comp.obj io.obj tree.obj db.obj : db.cpp db.h comp.obj : comp.cpp db.h io.obj : io.cpp db.h tree.h tree.obj : tree.cpp db.h tree.h
**clean: del .obj backup: copy .cpp f:\backup The last two rules have no effect unless specifically invoked:
nmake clean or nmake backup
db.exe
db.obj comp.obj io.obj tree.obj
db.cpp db.h comp.cpp db.h io.cpp db.h tree.h tree.cpp db.h tree.h