gcc - TimothyLe/SRE2017 GitHub Wiki

How to "work" your project

For the lack of a better name, this tutorial will direct you through the steps of creating a working project end-to-end. You will need:

  1. Build (make)
  2. Compile (gcc)
  3. Debug (gdb)

Ensure that you have a directory for your source code usually denoted as src a debug directory. You also need a .settings directory to include the language.xml file to specify what you will be programming in.

Compiling options

Source: Intro to GCC

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

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

Generate symbolic information for gdb and many warning messages.

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

Generate optimized code on a Linux machine.

g++ -O myprog.C -o myprog

Compile myprog.C when it contains Xlib graphics routines.

g++ myprog.C -o myprog -lX11

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