Using GCC/G++ to Compile C/C++ Programs


How to Compile and Run a C/C++ program

C Program (test.c) C++ Program (test.cpp)

#include <stdlib.h>

int main()
{
   printf("Hello World!\n");
   return 0;
}

#include <iostream.h>

int main()
{
   cout << "Hello World! "<< endl;
   return 0;
}

Command to compile C program Command to compile C++ program

gcc -o testc test.c


g++ -o testcpp test.cpp


Command to execute C program * Command to execute C++ program *

./testc


./testcpp

* Your executable file should automatically be created with the executable bit on.
  If it is not, type: chmod +x testc or chmod +x testcpp

More Useful gcc/g++ info

Frequently used compilation options

C and C++ compilers allow for many options for how to compile a program, and the examples below demonstrate how to use many of the more commonly used options. In each example, "myprog.cpp" contains C++ source code for the executable "myprog". In most cases options can be combined, although it is generally not useful to use "debugging" and "optimization" options together.

Compile myprog.cpp so that myprog contains symbolic information that enables it to be debugged with the gdb debugger.

g++ -g myprog.cpp -o myprog

Have the compiler generate many warnings about syntactically correct but questionable looking code. It is good practice to always use this option with gcc and g++.

g++ -Wall myprog.cpp -o myprog

Generate symbolic information for gdb and many warning messages.

g++ -g -Wall myprog.cpp -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.cpp -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.cpp -o myprog

Generate optimized code on a Linux machine.

g++ -O myprog.cpp -o myprog

Compile myprog.cpp when it contains Xlib graphics routines.

g++ myprog.cpp -o myprog -lX11
If "myprog.c" is a C program, then the above commands will all work by replacing g++ with gcc and "myprog.cpp" 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.cpp -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.cpp -o myprog -lefence

Compiling a program with multiple source files

If the source code is in several files, say "file1.cpp" and "file2.cpp", then they can be compiled into an executable program named "myprog" using the following command:

  g++ file1.cpp file2.cpp -o myprog 

The same result can be achieved using the following three commands:

  g++ -c file1.cpp
  g++ -c file2.cpp
  g++ file1.o file2.o -o myprog 

The advantage of the second method is that it compiles each of the source files separately. If, for instance, the above commands were used to create "myprog", and "file1.cpp" was subsequently modified, then the following commands would correctly update "myprog".

  g++ -c file1.cpp
  g++ file1.o file2.o -o myprog 

Note that file2.cpp does not need to be recompiled, so the time required to rebuild myprog is shorter than if the first method for compiling myprog were used. When there are numerous source file, and a change is only made to one of them, the time savings can be significant. This process, though somewhat complicated, is generally handled automatically by a makefile.


Thanks to http://galton.uchicago.edu/~gosset/Compdocs/gcc.html for some of the information above.