CMPT 225: Compiling with g++

CMPT 225: Compiling with g++


We will be using g++ to compile our cpp code this semester. Here is a brief tutorial on how to do this. This information can also be found in lecture slides.

Case 1: Compiling a simple c++ program

A simple c++ program like hello_world.cpp includes a main function and can be compiled as follows:

 g++ hello_world.cpp

The result of running this command is an executable file a.out that can be executed as follows:

 ./a.out

However, we usually prefer to name the executable file. For example, in the case of hello_world.cpp we may prefere to name the executable file hello_world.o instead of a.out. Here is how we can ask g++ to do this:

 g++ -o hello_world.o hello_world.cpp 

We can run the newly generated hello_world.o file as follows:

 ./hello_world.o

Remember that main functions can take arguments. For example hello_world.cpp may take a parameter name. In that case, we compile it as before but pass the paramters when running the executable file:

 ./hello_world John  

Case 2: Compiling a class and a cpp file that depends on it

Classes have headers (.h fiels) and implementations (.cpp files). While we usually include the headers in our cpp files, we need to tell the compiler where the implementation of the class is. When working with classes, we first compile the class, and then link them to the cpp file while compiling it.

For example, if we have a class intArrayList with header file intArrayList.h and implementation file intArrayList.cpp, and a simple test.cpp file with a main function that includes intArrayList.h, we can compile and run test.cpp as follows:

 g++ -c intArrayList.cpp   // this will generate an intArrayList.o file in the directory
 g++ -o test.o test.cpp intArrayList.o   // this will generate test.o 
 ./test.o

Obviously, if there are more than one user-defined classes that test.cpp includes, we should compile them all first, and then ask the compiler to link the object files to the test.cpp when compiling it. For example if test.cpp includes not only intArrayList but also a stringArrayList (introduced in stringArrayList.h and implemented in stringArrayList.cpp) and a floatArrayList (introduced in floatArrayList.h and implemented in floatArrayList.cpp), we can compile and run test.cpp as follows:

 
 g++ -c intArrayList.cpp   // this will generate an intArrayList.o file in the directory
 g++ -c stringArrayList.cpp // this will generate an stringArrayList.o file in the directory
 g++ -c floatArrayList.cpp // this will generate an floatArrayList.o file in the directory
 g++ -o test.o test.cpp intArrayList.o stringArrayList.o floatArrayList.o  // this will generate test.o
 ./test.o

g++ and different c++ standards

c++98, c++03, c++11, and c++14 are c++ standards that are currently in use. Some features that are accepted in a newer version may not be acceptable in a previous one. For example the "auto" keyword was introduced in c++11.

Sometimes, if you feel like a perfectly fine c++ program is not compiling, it may be because you're using an old standard. We can change the c++ standard using --std option in g++ command:

 g++ --std=c++11 -o hello_world.o hello_world.cpp // using c++11
 g++ --std=c++14 -o hello_world.o hello_world.cpp // using c++14
 g++ --std=c++0x -o hello_world.o hello_world.cpp // using an informal c++ standard before c++11  use this if later ones seem to be unavailable on your machine