How to Compile Programs for this C++ Tutorial
C. Henderson, University of Alabama
This page has instructions on how to compile the example programs supplied as part of the Brief C++ Tutorial. This tutorial was written to be used on a linux system with the gcc compiler suite and some kind of xterm interface.
This page has instructions on how to compile the example programs supplied as part of the Brief C++ Tutorial. This tutorial was written to be used on a linux system with the gcc compiler suite and some kind of xterm interface.
Phase 1: A Simple Single Program
The compiler provided on most linux systems for C++ programs is g++ (from GNU). To compile a simple hello_world.C program, you can simply do:
g++ hello_world.C
This will produce an executable file named a.out in the local directory. You can execute this program like any other linux command:
./a.out
(the ./ prefix is necessary unless you have the current directory in your $PATH variable. And you probably shouldnt have that, so ...)
When you execute this program, you should see the following output displayed back to your terminal:
|conor@conor c++_tutorial> ./a.out
Hello world!
Now, a.out is the default name given to the compiler output. But this isn't particularly useful, because it doesnt tell you anything at all about what the program does; also, if you later compiled a second program, it would have the same name, and therefore overwrite the file for the first one. To be more useful, we can make use of the '-o' option to the g++ compiler. This is a way to specify the name of the output program. So now we will call the output program file 'hello_world', as follows:
g++ -o hello_world hello_world.C
The output program file will now be created with the name hello_world, and we execute it as follows:
./hello_world
The name you give to the -o option is completely up to you. For example, you could do the following:
g++ -o goodbye_world hello_world.C
In this case, the output would be an executable named goodbye_world, and you would execute it by ./goodbye_world
g++ hello_world.C
This will produce an executable file named a.out in the local directory. You can execute this program like any other linux command:
./a.out
(the ./ prefix is necessary unless you have the current directory in your $PATH variable. And you probably shouldnt have that, so ...)
When you execute this program, you should see the following output displayed back to your terminal:
|conor@conor c++_tutorial> ./a.out
Hello world!
Now, a.out is the default name given to the compiler output. But this isn't particularly useful, because it doesnt tell you anything at all about what the program does; also, if you later compiled a second program, it would have the same name, and therefore overwrite the file for the first one. To be more useful, we can make use of the '-o' option to the g++ compiler. This is a way to specify the name of the output program. So now we will call the output program file 'hello_world', as follows:
g++ -o hello_world hello_world.C
The output program file will now be created with the name hello_world, and we execute it as follows:
./hello_world
The name you give to the -o option is completely up to you. For example, you could do the following:
g++ -o goodbye_world hello_world.C
In this case, the output would be an executable named goodbye_world, and you would execute it by ./goodbye_world
Phase 2: Working with Classes
Once we start working with classes, compiling our programs becomes a little more complicated.First we need to compile the source code for the class:
g++ -c Particle.cc
This makes an object file Particle.o
(The -c option tells g++ to stop at 'compile' and don't try to link).
Now we compile a program particle_example.C which makes use of this class. In this case, particle_example.C contains the main() routine that you want to execute. To make a working executable, you need to *link* in the object file for the class. You can do this with g++ by simply providing the object file on the commmand line when you are compiling the executable:
g++ -o particle_example.exe particle_example.C Particle.o
Here the output will be an executable called particle_example.exe
(Note that you dont need any option when you want g++ to link. The default is to link, so you only need options when you want to stop before linking, eg in the previous case where you just make an object file from a .cc source code).
If you are new to this style of programming, you may want to read in more detail about the compilation and link stages of making a running program, in a good reference.
Finally, if you are using a program that uses more than one class, then the object files for all classes need to be available for the linker. So for the example which uses the Proton class, derived from the Particle class, then you need to include both Proton.o and Particle.o for compilation:
g++ -o proton_example.exe proton_example.C Proton.o Particle.o
Note that when we scale up to large software projects, with many classes, related object files are usually placed together in a *library*. So in that case, instead of listing all the object files in the compiler command, you only need to link in the library, and the compiler itself will then select what it needs from the library.
Also, larger software projects generally have more sophisticated tools for compiling, which are beyond the scope of this simple tutorial.
g++ -c Particle.cc
This makes an object file Particle.o
(The -c option tells g++ to stop at 'compile' and don't try to link).
Now we compile a program particle_example.C which makes use of this class. In this case, particle_example.C contains the main() routine that you want to execute. To make a working executable, you need to *link* in the object file for the class. You can do this with g++ by simply providing the object file on the commmand line when you are compiling the executable:
g++ -o particle_example.exe particle_example.C Particle.o
Here the output will be an executable called particle_example.exe
(Note that you dont need any option when you want g++ to link. The default is to link, so you only need options when you want to stop before linking, eg in the previous case where you just make an object file from a .cc source code).
If you are new to this style of programming, you may want to read in more detail about the compilation and link stages of making a running program, in a good reference.
Finally, if you are using a program that uses more than one class, then the object files for all classes need to be available for the linker. So for the example which uses the Proton class, derived from the Particle class, then you need to include both Proton.o and Particle.o for compilation:
g++ -o proton_example.exe proton_example.C Proton.o Particle.o
Note that when we scale up to large software projects, with many classes, related object files are usually placed together in a *library*. So in that case, instead of listing all the object files in the compiler command, you only need to link in the library, and the compiler itself will then select what it needs from the library.
Also, larger software projects generally have more sophisticated tools for compiling, which are beyond the scope of this simple tutorial.
Extra Tips
Object files are binaries, but in case you ever need to, a useful way to look at what's in an object file is:
nm -C Particle.o
This will give an output showing the functions available from the object, eg:
000000c6 T Particle::GetMomentum()
000000e0 T Particle::SetMomentum(double)
00000098 T Particle::GetMass()
000000b2 T Particle::SetMass(double)
000000f4 T Particle::GetEnergy()
00000076 T Particle::Particle(double, double)
0000002a T Particle::Particle()
00000054 T Particle::Particle(double, double)
00000000 T Particle::Particle()
nm -C Particle.o
This will give an output showing the functions available from the object, eg:
000000c6 T Particle::GetMomentum()
000000e0 T Particle::SetMomentum(double)
00000098 T Particle::GetMass()
000000b2 T Particle::SetMass(double)
000000f4 T Particle::GetEnergy()
00000076 T Particle::Particle(double, double)
0000002a T Particle::Particle()
00000054 T Particle::Particle(double, double)
00000000 T Particle::Particle()