Your friendly command-line compiler, g++

Part 1 of A GNU/Unix development environment primer

The GNU compiler, g++, is all you ever really need to develop your C++ program. This section describes it and many of its options.

To compile a program, type g++ followed by options and filenames. For instance, to compile a program named ``prog.cpp'', type ``g++ prog.cpp'' at the shell prompt. This will place in the same directory a file named ``a.out'', which will be the executable version of that program.

That is all there is to using g++. Its other capabilities come through command-line options. All of the following described options are important to learn. (There are many other, arguably less useful options.) You can, and will, use a combination of several options at once during development.

-c
(compile only) The `c' option tells g++ to compile to object code only. The output will be the same filename with the suffix replaced by ``.o'', so the object code corresponding to ``prog.cpp'' will be ``prog.o''. (This can be changed with the `o' option.) Object code can then be used to form the actual executable. For instance, if your program has three source files ``foo.cpp'', ``bar.cpp'', and ``baz.cpp'', you can compile each into its object code (typing, for instance, ``g++ -c foo.cpp'') and then compiling the object code together on a final invocation of g++: ``g++ foo.o bar.o baz.o''.

Object code can save compilation time, which can be prohibitive for large projects. By compiling source files into object code before linking the source files together to form the actual executable, you can avoid recompiling files that remain unchanged between compilations.

Using the `c' option in conjunction with make (described below) makes for a powerful combination.

-g
(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would. This makes use of a debugger such as gdb much easier, since it will be able to refer to variable names that occur in the source code. This flag should always be included during development.
-l
(load library) Loads a library during the object code link phase. Many precompiled libraries exist for your use. Most significant among these is the math library ``libm.a'', which contains the functions defined in . (Other significant libraries include the curses terminal manipulation library ``libcurses.a'' and the X graphics library ``libX11.a''.) To compile a program which uses functions from the math library, type ``g++ foo.o -lm''. Note that the `l' option follows the names of your files, and that you should omit the initial ``lib'' and the suffix ``.a'' from the library name (so that in the case of ``libm.a'', you get only ``m'').
-o
(output) The `o' flag denotes the destination filename of the output of g++. If you want your executable file to be named ``prog'', you might type ``g++ -o prog foo.o bar.o''.
-D
(Define) The `D' flag allows you to define preprocessor constants on the command line. This is useful in debugging: you can set the DEBUG macro during the compilation of ``foo.cpp'' by typing ``g++ -DDEBUG foo.cpp''. To direct the preprocessor to replace occurrences of DEBUG with the value two, you would type ``g++ -DDEBUG=2 foo.cpp''.
-O
(Optimize) The `O' flag tells g++ that it should perform some optimizations on your code. Doing this can render runtime significantly faster, but it can also render compilation significantly slower, as optimization is a complex process. Generally - but not always - you should wait until your final debugging stages before setting this flag.
-Wall
(Warnings all}) The `Wall' flag tells g++ to turn on all warnings. These warnings frequently indicate possible coding errors that would be difficult to pin down as runtime errors. For this reason, you should always use the `Wall' flag: ``g++ -Wall foo.cpp''.

The other part of the art of using g++ is decoding its warnings and error messages, which at times can be downright cryptic. But that is subject matter for another time.

Last updated 16 Feb 1996.