CPSC 225, Spring 2002
About the g++ Compiler

For this course, you will be using the Linux g++ compiler. The textbook includes a copy of Microsoft's Visual C++, which you are welcome to try. However, I will not offer any support for using it. Visual C++ will work for many of the programs that you will write, although a few programs will use features that are specific to Linux. In any case, all programs must be in your Linux account to be graded.

To use the g++ compiler, you can write your program using a standard text editor such as nedit. The name of the file should end in ".cc" (or one of a half-dozen other extensions such as cpp, C, and cxx). If the file is named myprog.cc, then I recommend that you compile it with the command

              g++  -g  -Wall  -omyprog  myprog.cc

If no errors are found in the program, this will produce an executable program named myprog. You can run the program simply by typing in the name of the executable, myprog, as a command.

In this command, "-g", "-Wall", and "-omyprog" are all optional, although they are recommended. The "-g" tells the compiler to add debugging information to the executable. This information can be used by a debugger program such as kdbg, which we will encounter soon. A release version of a program is ordinarily compiled without this debugging information.

The "-Wall" tells the compiler to give warning messages about all constructions that the compiler considers suspicious. These warnings are not errors and will not stop the compiler from producing an executable. They indicate possible errors in programming logic, but can be ignored if you know that your code is correct.

The "-omyprog" tells the compiler to output an executable program named "myprog". You can use any name you want, although it is common to give the executable the same name as the source code, without the .cc. If you leave out this option, the executable will be named a.out, and you can run the program by entering a.out as a command. (In fact, I generally let the executable name default to a.out, since it's easy enough to rename the file when I have something I want to keep.)

Since it's easy to become lazy about including the "-g -Wall", you might want to create a command that includes these options automatically. To do this, edit the .bashrc file in your home directory and add this line to the file:

             alias c="g++ -g -Wall"

No spaces are allowed around the "=" sign! This defines "c" as an alias or abbreviation for "g++ -g -Wall", so that you can compile a program myprog.cc with "c myprog.cc" or with "c -omyprog myprog.cc". (The "." at the beginning of the filename ".bashrc" makes it a hidden file that you won't see in a directory window. To edit it, use the command nedit .bashrc on the command line.) You can define other aliases by adding them to .bashrc, if you want. Note that a change to .bashrc only becomes effective the next time you log in.

Later in the course, you will encounter other options that can be used with the g++ command, and you will learn how to use it to compile a program that is defined in several source code files.

David Eck, 4 January 2002