Saturday, May 30, 2009

Simplifying GCC

GCC is the GNU Compiler Collection which provides C, C++ etc compilers. These compilers are used by default in all *nixes .

Here i provide simple command line options which can prove to be quite useful.

  1. The simplest way to use GCC to compile a C source file is

    $ gcc -o test test1.c test2.c

    gcc is the C compiler, test1.c and test2.c are the input C source files and -o lets us specify the name of the output file. Here it is "test". Without the -o option, "a.out" is the default executable that gets created.

  2. The preprocessor:

    $ gcc -E test.c > test.out

    This option, ensures the compilation process stops after the pre-processor has run. This helps us in figuring out issues/problems in macros.


  3. The Compiler:

    $ gcc -c test.c -o test.o

    This option ensures the compilation process completes but doesn't invoke the linker/loader. This is useful if you want to just remove compilation warnings and errors.


  4. Header Files:

    $gcc -c test.c -I /location/of/header/files -o test

    Many a times the headers files you want to use, is located is some other directory. A "bad" practice followed is to include the direct path of the header files in the C src file.
    Instead use this option. It tells the compiler which directories to look in for the mentioned header files. The -I options can be used multiple times for multiple directories where header files are located.

  5. Library Files:

    $ gcc -c test.c -lpthread -L /usr/lib/libpthread


    Another requirement that is frequently required is using standard libraries ( NPTL Threads etc) or non-standard ones (expat etc). '-l' option tells which library to use while linking while '-L' tells where the find this library. In the above example during linking, it will search for pthread library in the dir /usr/lib/libpthread.

  6. Warnings, Errors, etc:

    $gcc test.c -o test -Wall -Werror


    -Wall options shows all warnings that are typically not shown during regular compilation. These errors are easy fixable like "Unused varniables", "implicit function declaration" etc. -Werror options tells the compiler to treat all warnings as errors and stop compilation instantly.
    Sometimes -Werror can be too strict for our purpose. Instead you can treat only certain warnings are errors.
    eg. -Werror-implicit-function-declaration: Treat only implicit function declaration warnings as errors. For more such options check the gcc man pages.

  7. Debugging:

    $gcc -g test.c -o test

    This option activates all the debugging symbols. This is required if one plans to use gdb for debugging (which is mostly the case).

  8. Optimizations:
    $gcc -O2 test.c -o test

    This option lets the compiler optimize the code . -O can take 0,1,2 levels of optimizations.
    More info is available in the man pages of gcc.
These options are the ones that are most frequently used. Obviously there are many more options available . Use them as per your needs and refer the man pages for the exhaustive list of options.

No comments: