optics.csufresno.edu Class Files - ECE 70 HOWTOs
Department of Electrical and Computer Engineering
Dr. Gregory R. Kriehn, Professor

HOWTO Compile a Program using gcc


The GNU Compiler Collection (usually shortened to GCC) is a compiler system supporting various programming languages produced by the GNU Project. GCC is a key component of the GNU toolchain. As well as being the official compiler of the GNU system, GCC has been adopted as the standard compiler by most other modern Unix-like computer operating systems, including GNU/Linux, the BSD family and Mac OS X. GCC has been ported to a wide variety of computer architectures, and is widely deployed as a tool in commercial, proprietary and closed source software development environments. GCC is also available for most embedded platforms, for example Symbian[3], AMCC and Freescale Power Architecture-based chips[4]. The compiler can target a wide variety of platforms, such as the Playstation 2[5] and Sega Dreamcast.[6] Several companies make a business out of supplying and supporting gcc ports to various platforms, and chip manufacturers today consider a gcc port almost essential to the success of an architecture.

Originally named the GNU C Compiler, because it only handled the C programming language, GCC 1.0 was released in 1987, and the compiler was extended to compile C++ in December of that year.[1] Front ends were later developed for Fortran, Pascal, Objective C, Java, and Ada, among others.[7]

The Free Software Foundation (FSF) distributes GCC under the GNU General Public License (GNU GPL) and the GNU Lesser General Public License (GNU LGPL). GCC is free software.

          -- Wikipedia Entry on the GNU Compiler Collection

(GNU is a recursive acronym for "GNU's Not Unix" and is pronounced "guh-NEW".)

Compiling Programs using gcc

In other words, gcc is a program that contains a C compiler for Unix/Linux-based operating systems.  The syntax for compiling C source code is given by:

gcc -lm [infile] -o [outfile]

As an example, if you wish to compile a source file, such as distance.c, and create an executable file called distance.o, type:

~> gcc -lm distance.c -o distance.o

If you wish to compile a hw1.c, type:

~> gcc -lm hw1.c -o hw1.o

Please note that gcc is includes both a compiler and a linker, and gcc is able to compile your code and link object files together in a single step. The -lm option is necessary whenever you wish to link the math library (which is supplied by the #include <math.h> statement in your code) to the executable that will be created. If you try to compile your code without including the -lm option and you are using the <math.h> header file in your code, gcc will fail to compile your code. The -o option is used to help specify the name of the output file.

Once compiled, you can execute your code by simply typing the name of your output file. The code will then execute:

~> hw1.o
The distance between points is:  3.61.

Please note that each time you make changes to your source code, you will have to re-compile and re-link your code. If your code fails to compile, it will provide a list of errors and the lines that they occur on. Fix any errors before trying to re-compile your code. Warning! Just because your code compiles does not mean that it will execute properly!