Compiling and running C++ programs under Unix


The command line interface

You might be familiar with only one way of interacting with a computer, based on graphical user interfaces and selection of menu items or buttons using a mouse. But that is only one way of interacting with a computer. The other common way is to use a textual, or command-line, interface. The command line interace requires that you know more, and that you type a little more, than the graphical user interface, so it is not as good for novices. But command lines tend to be much more flexible than a graphical user interface. You generally have more options available. Also, you can put commands together in scripts to make them do things that would be very difficult to do any other way.

To use a command-line interface, you need a terminal. In the Gnome interface to Solaris, you can get a terminal by clicking the right mouse button on the background and selecting new terminal. Type commands in the terminal window. Type the enter, or carriage-return, key to cause a command to be run.


Writing and editing programs

You might have used an integrated development environment that is a collection of several tools in one package. It is something like a stereo that contains a tuner, amplifier, cd player, etc. all in one.

We will use separate components instead, like a stereo that is built from separate components. The first component that you will need to use is a text editor. A text editor is used for creating all kinds of textual documents, not just programs.

You have a few text editors available.

  1. The system text editor is a simple editor similar to notepad in Windows. You can start it from Gnome by selecting Applications->Accessories->Text editor from the menu at th top of the screen. It should be self-explanatory. To open a file, either use open or drag the file into the text editor.

  2. There is a quite old Unix editor called vi. To use vi to edit file foo.cc, type command vi foo.cc in a terminal window. You are on your own to learn how to use this editor. Command man vi will show a manual on using vi.

  3. An alternative is emacs. Emacs is a large, versatile and extensible text editor. It has many options, and you can create your own commands as well. You can also have several files open at once and run a terminal from within emacs, which is convenient for using ssh. To use emacs to edit file foo.cc, type command emacs foo.cc& in a terminal window. To learn how to use emacs, see using emacs.

    One advantage of emacs is that it will indent C++ programs automatically. Typing a tab indents the current line. Also, when you type a right brace, it shows you the matching left brace, a feature that is very useful for keeping braces matching correctly.


Compiling a C++ program

The GNU C++ compiler is called g++. To run it, you use command g++, followed by options, followed by one or more files to compile. You should always ask the compiler to give you warnings. Warnings are advice that the compiler gives you telling you where you might have made a mistake. Pay heed to the compiler's advice. But always understand what is wrong before trying to fix it. Do not just blindly do what you think the compiler is telling you to do. To compile a program called assn1.cc, use the following command line.

  g++ -Wall -O -g assn1.cc
Option -Wall asks for all usual warnings to be turned on (Warnings ALL). Option -O asks the compiler to run the optimizer. Notice that this is a capital O, not the number 0 or lower case o. Option -g tells the compiler to produce extra information for a debugger to use.


Running your program

If compilation is successful, then the compiler will produce a file called a.out. This is the machine language version of your program. To run it, use command

   a.out

Note: A command is really just the name of a file. There is a file called g++ that holds the compiler. You can ask which file you will be running using the command

   which g++
You can give a longer name for a file as well. The current directory is called . (just a dot). If you run command
   ./a.out
then you are explicitly asking to run the program found in file a.out in the current directory. Try asking
   which a.out

The .bashrc file

There is a configuration file called .bashrc. (The first character of the file name is a dot.) Commands in this file are processed each time a new terminal window is created. You should have a standard .bashhrc file set up for you. If you have any troubles running commands, then your .bashrc file is probably wrong. Have somebody look at it.

You can put your own commands in your .bashrc file. For example, adding command

    alias compile="g++ -Wall -g -O -Wshadow"
then you can write
    compile prog1.cc
as an abbreviation for
    g++ -Wall -g -O -Wshadow prog1.cc