C-h iand select the ``GCC'' menu option). You don't need to know much about it for our purposes. This document is a brief summary. (Note that g++ and gcc are, for most practical purposes, identical programs. Running g++ is nearly the same as gcc -lg++. Consequently, when looking at GNU's online documentation using Emacs info, gcc is where you'll look.)
g++both to compile programs into object modules and to link these object modules together into a single program. It looks at the names of the files you give it to determine what language they are in and what to do with them. Files of the form
name.cpp) are assumed to be C++ files and files matching
name.oare assumed to be object (i.e., machine-language) files. To translate a C++ source file,
file.cc, into a corresponding object file,
file.o, use the
g++ -c compile-options file.ccTo link one or more object files,
file2.o, ..., to produced from C++ files into a single executable file called
prog, use the following command:
g++ -o prog link-options file1.o file2.o ... other-libraries(The options and libraries clauses are described below.)
You can bunch these two steps---compilation and linking---into one with the following command.
g++ -o prog compile-and-link-options file1.cc file2.cc ... other-librariesAfter linking has produced an executable file called prog, it becomes, in effect, a new Unix command, which you can run with
./prog argumentswhere arguments denotes any command-line arguments to the program.
Libraries are usually designated with an argument of the form -llibrary-name. In particular, -lg++ denotes a library of standard C++ routines and -lm denotes a library containing various mathematical routines (sine, cosine, arctan, square root, etc.) They must be listed after the object or source files that contain calls to their functions.
.ofiles from source files without doing any linking.
-Dname=value (Compilation option)
#define name valueat the beginning of the program. The `
= value' part may be left off, in which case value defaults to 1.
-ofile-name (Link option, usually)
g++(usually, this is an executable file).
-llibrary-name (Link option)
-g(Compilation and link option)
gdbinto the object or executable file. Should be specified for both compilation and linking.
make. Don't produce a
.ofile or an executable.
-pg(Compilation and link option)
gprofinto the object or executable file. Should be specified for both compilation or linking. Profiling is the process of measuring how long various portions of your program take to execute. When you specify
-pg, the resulting executable program, when run, will produce a file of statistics. A program called
gprofwill then produce a listing from that file telling how much time was spent executing each function.