The g++ Compiler

G++ is gnu's c++ compiler. It is the compiler that we will use for all examples and is the only compiler for which we will provide support. G++ is also the compiler that we will use to test and evaluate all of your programming assignments. Even if you use a different compiler for your code development, you must always move the project to your CS account and perform the final tests there. Use g++ to ensure of correctness in the environment your "customer" (that's us) will be using.

Compiling a Single File Program

Compiling a single file program is very easy. To try this, first get in your directory and be sure the file oop.cpp is there. (Do a file update otherwise). Then within that directory, try out a few g++ commands, as follows.

  1. First, try g++ sample.cpp. Now, do an ls (shows a directory's contents). There you will find a file called 'a.out'. Type a.out and watch the program that you just compiled run.
  2. Now, let's have g++ rename the executable to something more intuitive. Enter g++ -o sample sample.cpp. Now do an ls. You will see a new file called 'sample'. You can run this file by entering it on the same command line as 'a.out'.
  3. When you do an ls, you should see three files. They are sample.cpp, a.out, and sample. The a.out file was created from the first compile and the sample file was created from the second compile. Let's get rid of both of them to eliminate confusion. Type rm a.out sample to remove those files.
  4. Now, let's compile object code from the file sample.cpp. Use g++ -c sample.cpp to do this. Now, do an ls and see what files are there. By using the '-c' option, you have created object code. By default, the compiler has named it 'sample.o'. Try executing 'sample.o' the same way you did 'sample' or 'a.out'. You should get an error message stating sample.o: Permission denied.  This happened because 'sample.o' is not an executable. It has simply been compiled down to object/binary code but has not yet been linked.
  5. Let's link the file 'sample.o' with all the information it needs to execute. Do this by entering g++ -o sample sample.o. Now you can execute 'sample' as you did before.
  6. At this point you might be asking yourself, "Why would I ever want to create object code? It's clearly simpler to go straight to an executable." That's a good question. If you're only creating an executable from a single file, there isn't any reason to make object code. But, all of the projects in this course will involve multiple files. By creating object code, you can create a mini library of code that need not be recompiled every time you want to make a change to only one file. All you need to do is change one file, and then compile it and link them together.
  7. If you're following along with the slide in the upper window, you will see that we haven't given an example for the '-l' flag. In this course you will probably never need to use that flag. But we've included it simply for completeness. Since you may need it some day, it's better to have some exposure to it.

Compiling a Multi-File Program

Most coding projects will involve multiple files. When working with multiple files the question of how to include properly files always comes up. As you probably know there are two standard ways to include files. They look like:

    #include <file1.h>    // (1)
    #include "file.cpp"   // (2)

You are used to seeing style (1) used for standard libraries. In early programming courses, often students use style (2) for their own files. For this course, you must use style (1) to receive full credit on your projects. To make this stye work the way you intend it to, you must tell the compiler where to look for the "include" files. This is where the '-I' flag comes in. Anything that follows immediately after the '-I', no spaces, will be searched. If you need to search more than one directory, simply use multiple '-I' statements.

Let's try compiling the multi-file listed on the slides. It is assumed that these files have been created (using a text editor such as Emacs) and are in the current working directory. Below are actual contents of the four files:

// file1.h
const int magic_number = 1492;
long multiply_by_MN (int number);

// file1.cpp
#include <file1.h>
long multiply_by_MN (int number) {
  return (long)(number * magic_number);
}

// main.h
constant int loop_n_times = 4;

// main.cpp
#include <iostream.h>
#include <file1.h>
#inlcude <main.h>
int main() {
  int sum = 0;
  for (int i = 0; i < loop_n_times; i++) {
    sum += mulitply_by_MN (13);
  }
  cout << "Final number is" << sum << "\n";
  return 0;
}

Remember, you can't translate .h files to object or executable code. These are header files used to inform the compiler how to interpret and translate the code in a .cpp file. (An exception is when the file contains template code. Then the header file contains the implementation, so there is no .cpp file. Even in this case, however, you can't make workable object code from the header file.) Compiling the object code would require a statement like g++ -c file.cpp, but that causes the error statement file1.cpp:2: file1.h: No such file or directory.

Why did this happen? When you use the " "'s to include a file, the compiler looks in the current directory to find the files. Basically you're telling the compiler where to find the file. When you use the <>'s to include a file, the compiler searches all the places it expects to find include files. But it doesn't search your current directory by default. That's why you get the error message. To solve this problem, use the '-I' flag like this: g++ -c -I. file.cpp. Now when you do an ls you see the 'file.o' file.

Now, compile 'main.cpp' the same way, g++ -c -I. main.cpp. To create the executable, link the pieces together and name the output, g++ -o sample main.o file.o. This creates an executable named 'sample'. Try running it by typing sample at the command prompt.

Comments:  If you change one of the .cpp files, for instance 'file1.cpp', you need only recompile it and then relink all the pieces. There is no need to recompile 'main.cpp'. This is the benefit of using .o files. Finally, be wary of using 'test' or 'exec' as the name of your executable. There are standard unix commands that already have these names. They will be called instead of the ones you created. If you think this type of conflict might be occurring with some name you have chosen, you can enter ./<filename> to guarantee that your version is executed.

Additional Information

The man pages are the best place to get more information about g++. To view the man pages for g++, type man g++ at the command prompt. G++ is a specialized front end for the gcc compiler. Gcc compiles c code. If you can't find what you want in g++, try man gcc. Most c and many c++ functions also have man pages, so you should get comfortable using them.