How To Compile
How To Compile
Introduction
There is a different between compiling and linking your program, a difference that if
you learned programming from integrated environments you might not be aware of it.
Compilation involves converting your source code to object form, but not joining
multiple object files into an executable. So, in C/C++, each source file can be
compiled independently of other source files that go into a project. When all
individual source files are compiled, then you can "join" them into an executable, this
is called linking the project.
To explain how to do each of these steps, follow the directions below. For this
discussion I assume that $ this is the prompt of your operating system. Also, the gcc
compiler compiles C and C++. It makes the determination of which compiler to use
depending on the file name extension. For that purpose, I use .cpp to let the compiler
know that the file is a C++ file. There are other extension that the compiler recognizes
as C++ also, but this is the most clear of all. In some environments, there is a symbol
defined as either g++ or c++ that just runs the gcc compiler directly. I use g++ in the
discussions here, but we are really running the gcc compiler.
You also have the choice of specifying the name of the file that will contain the
executable by using the -o option.
$ g++ file.cpp -o prog1
This will compile file.cpp, link the executable and place it in the file named prog1.
The following command will only compile the file.cpp. It will not try to run the linker to
create an executable. The idea is that if you have multiple source files, you can
compile them one by one, and recompile only those that have changed since the last
compilation. This command uses the -c option which tells the compiler, compile only
the file. The object file that results is placed in file.o
$ g++ -c file.cpp
Imagine that your project consists of two files, file.cpp and other.cpp. You can build
the full executable in any of the following ways:
Compile them both at the same time and place result in a.out
$ g++ file.cpp other.cpp
Compile them both at the same time and place results in prog2
$ g++ file.cpp other.cpp -o prog2
Compile each separately and then link them into a.out
$ g++ -c file.cpp
$ g++ -c other.cpp
$ g++ file.o other.o
Compile each separately and then link them into prog2
$ g++ -c file.cpp
$ g++ -c other.cpp
$ g++ file.o other.o -o prog2
File Extensions
• always use cpp as the extension for your source files
• always use h as the extension for your header files
• use something other than cpp for your template files (you might use tcc)
• always put #ifndef around your header files to avoid double definitions and to break
circular definitions. Your header files will look like this, assume that the file name is
data.h, then:
// data.h
// other header information
#ifndef __DATA_H__
#define __DATA_H__
#endif
Make sure that the symbol used in the ifndef is unique in your whole projects. The
best thing to do is to use the file name with the "." replaced by an underscore and
adding other underscores around to make it more unique.
Be careful if you copy a header file to use as a template to create another one. If you
do this, make sure you change the symbol, having two header files with the same
ifndef symbol will in effect hide the second you include in every compilation.
• include in your header file only those other header files that are needed in the
header itself, not for the source associated with it.