Compiling C and C++ Programs: Example 1: Compiling A Simple Program
Compiling C and C++ Programs: Example 1: Compiling A Simple Program
11/4/13, 12:21
This command compiles hello.C into an executable program named "hello" that you run by typing 'hello' at the command line. It does nothing more than print the word "hello" on the screen. Alternatively, the above program could be compiled using the following two commands.
g++ -c hello.C g++ hello.o -o hello
The end result is the same, but this two-step method rst compiles hello.C into a machine code le named "hello.o" and then links hello.o with some system libraries to produce the nal program "hello". In fact the rst method also does this two-stage process of compiling and linking, but the stages are done transparently, and the intermediate le "hello.o" is deleted in the process.
Have the compiler generate many warnings about syntactically correct but questionable looking code. It is
http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html Page 1 of 3
Gcc tutorial
11/4/13, 12:21
good practice to always use this option with gcc and g++.
g++ -Wall myprog.C -o myprog
Generate optimized code on a Solaris machine with warnings. The -O is a capital o and not the number 0!
g++ -Wall -O -mv8 myprog.C -o myprog
Generate optimized code on a Solaris machine using Sun's own CC compiler. This code will generally be faster than g++ optimized code.
CC -fast myprog.C -o myprog
If "myprog.c" is a C program, then the above commands will all work by replacing g++ with gcc and "myprog.C" with "myprog.c". Below are a few examples that apply only to C programs. Compile a C program that uses math functions such as "sqrt".
gcc myprog.C -o myprog -lm
Compile a C program with the "electric fence" library. This library, available on all the Linux machines, causes many incorrectly written programs to crash as soon as an error occurs. It is useful for debugging as the error location can be quickly determined using gdb. However, it should only be used for debugging as the executable myprog will be much slower and use much more memory than usual.
gcc -g myprog.C -o myprog -lefence
The same result can be achieved using the following three commands:
g++ -c file1.C g++ -c file2.C g++ file1.o file2.o -o myprog
The advantage of the second method is that it compiles each of the source les separately. If, for instance, the above commands were used to create "myprog", and "le1.C" was subsequently modied, then the following commands would correctly update "myprog".
http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html Page 2 of 3
Gcc tutorial
11/4/13, 12:21
Note that le2.C does not need to be recompiled, so the time required to rebuild myprog is shorter than if the rst method for compiling myprog were used. When there are numerous source le, and a change is only made to one of them, the time savings can be signicant. This process, though somewhat complicated, is generally handled automatically by a makele.
http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html
Page 3 of 3