Using Microsoft Developer Studio for C++ Projects Developer Studio is used as a common programming environment for Visual C++ and J++. To start Developer Studio, click on the small icon for it that is found on the Microsoft Office toolbar at the top of the desktop. You can also select Start, Programs, Microsoft Visual C++, but that takes longer. 1. Creating a New Workspace It is recommended that you use the same PC each time if at all possible. The first time you start Developer Studio you should create a new workspace. Then each time you write a program you will add it's project to this one workspace. This allows Developer Studio to show you a list of all of your projects and to easily select among them. Select File, New. Click on the Workspace tab. (Be sure to always check that you have selected the correct tab. Otherwise you may create some other type of new item.) For the filename, it is suggested that you use your initials, such as abc. For the location use c:\fpuser\startup as that is the only directory in which you can save files on the lab's hard drives. Do not use a diskette for this, as you will quickly run out of room. Unless you have to switch to a different PC you should not have to create another workspace for the rest of the course. 2. Creating a New Project A project groups together all of the files associated with one of your programs and stores them in the project's directory. Especially when we start creating programs with the source code stored in more than one file it will be important to group things into projects. However, even for a program with a single C++ source file, it helps a lot to use a project to keep things separate from the files for other programs. Select File, New. Click on the Projects tab. Click on "Win32 Console Application". Fill in a project name, which can be the same name as the main source file (without the .cpp). For example, pg4_12 would be a reasonable project name. The default location should be correct, but check that it is set to your workspace directory within c:\fpuser\startup. Make sure the radio buttons are set to select "Add to current workspace". Do NOT use "Create a new workspace". The Dependency check box should NOT be checked. Finally, click OK. 3. Creating a New C++ Source File In your project you will want to have at least one C++ source file containing the code for your program. If you have just created a new project, that project will probably already be active. Select Project, Set Active Project to see what project is currently the active one. If it is not the one to which you want to add your C++ source file, click on the correct one. Once you are sure that you have the correct project active, select File, New. Click on the Files tab and click on "C++ source file". Then fill in the desired file name. The .cpp extension will be added automatically; there is no need to type that. The location should automatically be set to the desired project directory, but glance at it to be sure. The "Add to Project" box should be checked and the correct project name should be shown next to it. Finally, click on OK. You should then be in the editor, editing a blank .cpp document with the correct name. Type in your desired code. Select File, Save to save your file. You can also copy in code from another file by selecting Insert, File as Text. It is also possible to add code to a new file by using Cut, Copy, and Paste (found under Edit) to bring in code from another file. Sometimes you will want to use more than one C++ source file. It is typical to place the main function in one source file, other stand-alone function in another source file, and functions belonging to a user- defined class in their own source file. All of these files that belong to the same program must be added to the same project. 4. Creating a New C++ Header File A header file is a file with a .h extension. It is typically used to contain constants, types, class declarations, function prototypes, etc. Executable code is usually NOT placed in header files. To add a new header file to a project, one follows almost the same steps as in creating a C++ source file. Begin by checking that the correct project is active. Then select File, New. Click on the Files tab and click on "C/C++ header file". The rest proceeds much as above. 5. Compiling and Running a Program Once you have created a project and its associated C++ source and header file(s), you are ready to compile. Actually, the best choice is "Rebuild All", found under the Build menu item. The "Compile" option would only handle a project with a single source file. "Rebuild All" will see that everything is recompiled, linked, etc. Error messages will be shown in a small window at the bottom of the screen. Of course, you can enlarge this window by placing the mouse on the dividing line between this window and the one above and dragging it. If this window is not shown at all, try clicking on the "Output" button in the middle of the toolbar. Double clicking on an error message should take you to the spot in the program where the error occurred. To run your program, either click on the button with the red exclamation point, or select Build, Execute. Since we are creating "console" applications, the programs run in a DOS-like window showing all input and output. 6. Saving Your Work on Diskette Always save a copy of any .cpp and .h files on diskette. You never know when someone might erase some of your files from the machine in the lab or when the hard drive might fail. It is suggested that you use Windows Explorer to drag the desired files to your diskette. If some of your source files for different projects have the same name, just create directories on your diskette, one for each project, and place the source files into the correct directories. On a homework assignment it is important not to leave your .cpp and .h files behind on the lab PC. Someone else could then easily copy your work. Before leaving the lab, copy the .cpp and .h files associated with your homework to diskette. Check that they have been correctly saved on the diskette and then delete those files from the hard drive. (Using Windows Explorer, just select the files and press the Delete key.) Your other workspace and project files can remain on the hard drive for the next time you use the lab. 7. Returning to Your Workspace, Project, and Files If you moved your source files to diskette, the first step would be to copy them back to the correct project directory within your workspace directory. For example, the location might be something like c:\fpuser\startup\abc\pg8_13, where abc is your workspace directory and pg8_13 is the desired project directory. Start Developer Studio. Select File, Open Workspace. Do NOT use Open. In the dialog window, move to your workspace directory, say c:\fpuser\startup\abc, and then double click on the .dsw file of the same name that resides in this directory. (In our example the file would be named abc.dsw.) This opens your workspace and places you right where you left off within it. If the file that you want to work on is not already visible, you can use File, Open to bring it into the editor. However, it is easier to use the Files View usually shown in a window on the left. If this window is not visible, try clicking on the Workspace button near the middle of the toolbar at the top of the screen. At the bottom of this window should be three tabs, labeled C, F, and I. Clicking on the F tab gives you the File View of you entire workspace with all of its projects. Look for your current project. If there is a little box with a + in front of the line for your project, click on it to have the project's files listed. Then double click on the desired file to bring it into the editor. To close a file that you no longer want on the screen, click the lower of the two x buttons at the top right of the screen. (The upper x button closes Developer Studio itself.) You may have several files in the editor and want to close them all before opening another one. 8. Using a Different PC If you ever need to use a different PC, create a new workspace for yourself on it as above. Then create a new project for whatever you are working on. If you already started this project on another machine, just use the same name for the project on this machine, copy in the .cpp and .h files for this project, and add them to this project by using Project, Add to Project, Files. Of course, first make sure that the correct project is active. 9. Debugging a Program There are many ways to debug a program. One of the simplest is to use cout to output the values of key variables at various places in the program.. Another is to use the debugger. You can access it by selecting Build, Start Debug. This brings up the choices: Go (to execute until a breakpoint is reached), Step Into (to execute the program line by line), Run to Cursor (run to the current cursor location). If you select Step Into or Run to Cursor, you will be given a debug toolbar that has other options such as Step Over (also for executing line by line but without going inside the code for any function that are called). This toolbar also allows you to restart the program, to end debugging, etc. Note that at the bottom left of the screen is a window showing the values of variables being used in the line just executed. On the bottom right is a window where you can enter the names of variables whose values you would like to see. Note that to add or remove breakpoints you can use the button with the picture of the hand on it. The debugger has many more features which you can learn if you like. 10. Seeing Your Workspace Files and Classes Under item 7 above, the File View of your workspace was already mentioned. This is normally shown on the left of your screen. With File View you can scroll through a tree showing all of your source files, organized by project. If you click on the I tab you get Info View which gives you access to a lot of Microsoft documentation (also available under Help). Clicking on the C tab gives you the class view. Even if your program does not use classes, this view can be helpful in that it shows you the names and parameter info for all of your functions. If there is a box with an x in front of your project, click on the x to expand things to show the classes inside of your project. Then click on the x in front of a class name to see a list of the functions in that class. Double clicking on the class name will take you to the class declaration. Double clicking on a class function name will take you to the code for that function. The data fields for the class are also shown. Under the directory labeled Globals you will find the stand-alone functions and global variables used by your project. Another useful tool is the Wizard Bar, which can be found a few items to the left of the ! button on the toolbar. It has a button for a default action and a downward triangle for displaying other possible class- related actions (such as go to function definition, add member function, add new class, etc.). The Wizard also uses the three windows on the rest of the line to the left of this. Try it out as it gives a convenient way of working with classes. 11. Customizing Developer Studio can be customized in various ways. Please resist the temptation to do so as it will only make things difficult for others who use it. In particular, do not use Tools, Customize or Tools, Options. You may, however, change things that only affect your own project. Look under Project, Settings. Here you can find a lot of detailed settings. Some of these, for example, can be used to omit debugging info, to give a faster executable, etc. Although you may change these settings, do so at your own risk. The default settings will almost always be appropriate. 12. Printing The default printing from Developer Studio uses a small font that makes the printout hard to read. You are likely to have better results by starting Word, opening your program file in it, making any desired formatting changes, and printing the file. Do not save it in Word, of course.