Creating a new C++ project
This page describes briefly the steps needed to make a new C/C++ project that uses the glut OpenGL library routines. The same steps are used, in fact, for any program which has a main() program as its entry point. We will describe the simplest possible way to do this. For more information consult the online documentation provided with the Microsoft C++ compiler.
When writing programs for homework assignments, you will in be modifying an existing workspace/project to create a new application. However, you also will sometimes need to create a completely new project -- we describe these steps below. We also describe how to add source files and header files to a project.
The Microsoft C++ compiler, like most integrated development environments, combines source files into a project. Also it combines projects into a "workspace". The projects are a collection of source code and other resources, such as icons, menus, and other widgets.
Step 1: Create a workspace and project.
Have the Visual C++ compiler running and as the active window.
If you have a workspace currently open, close it from the the File menu.
Then under the File menu, choose New.... A dialog box opens: choose the Projects tab. Select Win32 Console Application as the project type. Choose a project name (something informative, but not too long), and a directory for a location (the C++ compiler will automatically create a folder whose name is the same as the project). Keep Create new workspace and Win32 selected. Press OK.
In the next box, select either Empty Application, Simple Application or Hello-World Application. (Empty Application is nearly always best.)
Step 2: Add source and header files to the workspace.
You may start by creating new .c, .cpp, and .h
files in the usual way (use New from the File menu).
Use the extension .cpp if you are writing in C++, and use .c
for C files. Since C++ is a superset of C, it is generally easiest to just use
Once a source file is created, you must add it to the project. You do this by selecting the Project menu, then Add to Project, then Files....
Alternately, instead of creating a new file via the File menu and later adding it to the project, you may create a new file that is already in the project by using the Project menu, then Add to Project, then New....
[There are various other, shortcut methods of adding a file to a project: you may try to compile it and it will prompt you to put it in the project. Or you may try right-clicking on the source file window.]
Step 2': Modifying other source files.
Instead of creating a completely new source file, you may wish modify an
existing source file for your new project. (For instance, another OpenGL program,
that you want to modify). To do this, first create a copy of the source file
and put it in your new project directory. Then select the the Project
menu, then Add to Project, then Files... to add it to
the project. Don't forget to use the same procedure for header files.
If you skip the step of making a new copy of the file before adding it to your project, then you will have the same source file in two different projects. If you modify the file for one project, it will also change the other project and in many cases, this is not what you want. (On the other hand, if you have a large system with multiple projects sharing the same code, perhaps it is what you want...)
Use the method of Step 2' to add additional source files to your workspace as needed. The C++ compiler has a "Workspace" window on the left side of the screen. Switch to the FileView mode in this window to have convenient access to the different files in your project.
Step 3: Tell the Linker about the OpenGL libraries. (may be optional)
For OpenGL, you need to tell the compiler (or to be precise, the linker) to include the OpenGL libraries. If you do not do this, then you will get lots of error messages about unresolved external references. This is done by selecting the Project menu, then Settings..., and then in the dialog box, Link. This shows a dialog box with a editable field Object/Library modules. Add to the end of the contents of this field, the following three OpenGL library names:
opengl32.lib glu32.lib glut32.lib
You should do this for both the Debug and Release versions of your project.
If you would rather not have to do the above, you can also use the #pragma command to tell the compiler which libraries to use: the newer versions of glut32.h use the pragma commands, and this is why you may be able to skip this step.
Step 4: Compiling and Linking.
Compile your program using the Build menu or the shortcut key F7. Run your program, with from the Build menu, or with the shortcut key F5. Once in a while, the C++ compiler gets confused and writes corrupt files. Running Rebuild All will fix this.
Using I/O (stdin/stdout): If you run your program it will open a text window (in addition to any OpenGL window). The standard C I/O routines, such as printf, may be used to read and write from the window, via stdin and stdout. If you want to do this, don't forget to include the necessary C header files.
Debug and Release modes. Debug mode allows you to debug your program (of course!). Release mode does not, but runs substantially faster, say three times as fast!
1. If you set your project up the wrong way, you may get compile-time errors about the use of precompiled headers. If this happen, use the Project menu, go to Settings..., then choose the C/C++ tab, select Category Precompiled Headers and turn off precompiled headers.
2. If you are using a home computer or other PC besides the lab computers, you may need to get the GLUT header files and libraries. There are three of these: glut.h, glut32.lib and glut32.dll. They are available from various locations on the web.