Stanford CS 106L - Chapter 1 - Getting Started

Unformatted text preview:

Chapter 1: Getting StartedChapter 1: Getting Started_________________________________________________________________________________________________________Every journey begins with a single step, and in ours it's getting to the point where you can compile, link, run, and debug C++ programs. This depends on what operating system you have, so in this section we'll see how to get a C++ project up and running under Windows, Mac OS X, and Linux.Compiling C++ Programs under WindowsThis section assumes that you are using Microsoft Visual Studio 2005 (VS2005). If you are a current CS106B/X student, you can follow the directions on the course website to obtain a copy. Otherwise, be prepared to shell out some cash to get your own copy, though it is definitely a worthwhile investment.* Al-ternatively, you can download Visual C++ 2008 Express Edition, a free version of Microsoft's development environment sporting a fully-functional C++ compiler. The express edition of Visual C++ lacks support for advanced Windows development, but is otherwise a perfectly fine C++ compiler. You can get Visual C++ 2008 Express Edition from http://www.microsoft.com/express/vc/. With only a few minor changes, the directions for using VS2005 should also apply to Visual C++ 2008 Express Edition, so this section will only cover VS2005.VS2005 organizes C++ code into “projects,” collections of source and header files that will be built into a program. The first step in creating a C++ program is to get an empty C++ project up and running, then to populate it with the necessary files. To begin, open VS2005 and from the File menu choose New > Pro-ject.... You should see a window that looks like this:* I first began programming in C++ in 2001 using Microsoft Visual C++ 6.0, which cost roughly eighty dollars. I re-cently (2008) switched to Visual Studio 2005. This means that the compiler cost just over ten dollars a year. Con-sidering the sheer number of hours I have spent programming, this was probably the best investment I have made.- 12 - Chapter 1: Getting StartedAs you can see, VS2005 has template support for all sorts of different projects, most of which are for Mi-crosoft-specific applications such as dynamic-link libraries (DLLs) or ActiveX controls. We're not particu-larly interested in most of these choices – we just want a simple C++ program! To create one, find and choose Win32 Console Application. Give your project an appropriate name, then click OK. You should now see a window that looks like this, which will ask you to configure project settings:Note that the window title will have the name of the project you entered in the previous step in its title; “Yet Another C++ Program” is a placeholder.At this point, you do not want to click Finish. Instead, hit Next > and you'll be presented with the follow-ing screen:Chapter 1: Getting Started - 13 -Keep all of the default settings listed here, but make sure that you check the box marked Empty Project. Otherwise VS2005 will give you a project with all sorts of Microsoft-specific features built into it. Once you've checked that box, click Finish and you'll have a fully functional (albeit empty) C++ project.Now, it's time to create and add some source files to this project so that you can enter C++ code. To do this, go to Project > Add New Item... (or press CTRL+SHIFT+A). You'll be presented with the following dialog box:- 14 - Chapter 1: Getting StartedChoose C++ File (.cpp) and enter a name for it inside the Name field. VS2005 automatically appends .cpp to the end of the filename, so don't worry about manually entering the extension. Once you're ready, click Add and you should have your source file ready to go. Any C++ code you enter in here will be considered by the compiler and built into your final application.Once you've written the source code, you can compile and run your programs by pressing F5, choosing Debug> Start Debugging, or clicking the green “play” icon. By default VS2005 will close the console win-dow after your program finishes running, and if you want the window to persist after the program finishes executing you can run the program without debugging by pressing CTRL+F5 or choosing Debug > Start Without Debugging. You should be all set to go!Compiling C++ Programs in Mac OS XIf you're developing C++ programs on Mac OS X, your best option is to use Apple's Xcode development en-vironment. You can download Xcode free of charge from the Apple Developer Connection website at http://developer.apple.com/.Once you've downloaded and installed Xcode, it's reasonably straightforward to create a new C++ project. Open Xcode. The first time that you run the program you'll get a nice welcome screen, which you're free to peruse but which you can safely dismiss. To create a C++ project, choose File > New Project.... You'll be presented with a screen that looks like this:Chapter 1: Getting Started - 15 -There are a lot of options here, most of which are Apple-specific or use languages other than C++ (such as Java or Objective-C). In the panel on the left side of the screen, choose Command Line Utility and you will see the following options:Select C++ Tool and click the Choose... button. You'll be prompted for a project name and directory; feel free to choose whatever name and location you'd like. In this example I've used the name “Yet Another C+- 16 - Chapter 1: Getting Started+ Project,” though I suggest you pick a more descriptive name. Once you've made your selection, you'll see the project window, which looks like this:Notice that your project comes prepackaged with a file called main.cpp. This is a C++ source file that will be compiled and linked into the final program. By default, it contains a skeleton implementation of the Hello, World! program, as shown here:Chapter 1: Getting Started - 17 -Feel free to delete any of the code you see here and rewrite it as you see fit.Because the program we've just created is a command-line utility, you will need to pull up the console win-dow to see the output from your program. You can do this by choosing Run > Console or by pressing ⇑ R. Initially the console will be empty, as shown here:⌘Once you've run your program, the output will be displayed here in the console. You can run the program by clicking the Build and Go button (the hammer next to a green circle containing an arrow). That's it! You now have a working C++ project.If


View Full Document

Stanford CS 106L - Chapter 1 - Getting Started

Download Chapter 1 - Getting Started
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 1 - Getting Started and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 1 - Getting Started 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?