Compiling and Running C++ Programs with Visual Studio 2005 - Project | CS 1410, Study Guides, Projects, Research of Computer Science

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

Pre 2010

Uploaded on 07/23/2009

koofers-user-ck9-1
koofers-user-ck9-1 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Compiling and Running C++ Programs with Visual Studio 2005
Document Information
1. Current revision: 2008-01-05
2. Location of latest revision: http://icarus.cs.weber.edu/~dab/cs1410/studio.pdf
3. Corrections & Suggestions: [email protected]
4. Contents
Contents
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
Checklist
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)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Compiling and Running C++ Programs with Visual Studio 2005 - Project | CS 1410 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Compiling and Running C++ Programs with Visual Studio 2005

Document Information

  1. Current revision: 2008-01-
  2. Location of latest revision: http://icarus.cs.weber.edu/~dab/cs1410/studio.pdf
  3. Corrections & Suggestions: [email protected]
  4. Contents

Contents

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

Checklist

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)

Visual Studio / Integrated Development Environment (IDE) Operation

Visual Studio and C++: Step-By-Step

  1. Start the IDE a. Start º All Programs º Microsoft Visual Studio 2005 º Microsoft Visual Studio 2005 - OR -

b. Double-click the desktop short cut (if available)

  1. Create A New, Empty Project a. File º New º Project (illustration at left) - OR - b. Press the New Project Button (illustration at right)
  2. Make a Console Project a. Click the “+” adjacent to “Visual C++ Projects” b. Select “Win32” c. Select “Win32 Console Project” d. Enter a project name i. this will become the name of the folder containing the project ii. it will also become the name of the executable program iii. the “Solution Name” will be the same as the project name (you may change the solution name but it is not necessary) e. Enter the location of the project (this may be on a flash drive) f. Press “OK”
  1. Add A New File to the Project a. Project º Add New Item (illustration at left) b. Note that selecting File º New º File will allow you to create a new file but it is not placed in the project - OR - c. Press the Add New Item button, which is added to the toolbar once a project is opened (illustration at right)

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

  1. Compile and Run the Program a. Compile: Build º Build Solution (illustration at left) b. Run: Debug º Start Without Debugging (illustration at right)

Additional C++ Tasks

  1. Add an existing file to an existing project (useful for moving files between home and school) a. Press the – button attached to the second button from the left (illustrated at the right) b. Select “Add Existing Item” - OR - c. Project (on the menu bar at the top) º Add Existing Item - AND - d. Navigate to the location of the files (e.g., on a flash drive) either with the “My Computer” icon or by pressing the – button at the top of the window (illustrated below) e. Select the file name in the window f. Press the “Add” button g. Note that this does not copy the file into the project directory – it creates a link to the existing 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

Command Line Operation

  1. Opening a Command Prompt Window (with the command line compiler in the PATH) a. Start º All Programs º Microsoft Visual Studio 2005 º Visual Studio Tools º Visual Studio 2005 Command Prompt

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

  1. (Alternate to 1 above) Configure and use a Command Prompt window a. Open a “Command Prompt” window: Start º All Programs º Accessories º Command Prompt b. Run the following command at the prompt (this command reflects the default installation location; also note that the quotation marks are part of the command): "C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32" c. This command configures only the current command prompt window: it sets PATH, INCLUDE, and LIB environment variables. If this window is closed, the command must be re-run in a new window
  2. (Optional) Create a desktop shortcut to open a Commands Prompt window that is automatically configured to use the command line compiler ( for instructors : it is also useful to configure a window with larger text)

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

  1. (Optional) Permanently set the PATH, INCLUDE, and LIB environment variables to support the command line compiler. (NOTE: an error in the PATH environment variable can make it difficult or impossible to run installed programs. If you make a mistake, press the “Cancel” button.) a. Open the System Properties panel: Start º Control Panel, double-click System b. Choose the Advanced tab and press the Environment Variable button (left illustration) c. In “System variables,” select the PATH environment variable and press Edit (right illustration)

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

Makefiles and The make/nmake Command

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

Makefile Format

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 command-to-build-target-out-of-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.

How Make Works

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.

General Rules

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.

Other Tricks

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