Gcc - the "Gnu Compiler Collection" - is used to compile AND to link C language programs in an open source environment. A single command -
gcc runs a series of phases that take you from source code through to an executable program file, with the actual steps taken works out based on:
• the extensions you give for the files
• command line options
Here are the phases:
1. The C preprocessor (cpp)
2. The C Compiler
3. The Link Loader (ld)
The
C Preprocessor takes directives in the source file (that's lines that start with a # character) and acts on them as appropriate - "including" standard files of definitions, replacing "defined" constants via macros, and so on.
The
C Compiler takes the preprocessor output and converts it from an ASCII English-like source code into binary code suitable for running on the particular target machine.
But is is NOT a complete program yet - just a section of code / a component that needs to be joined to other components.
The last phase - the
Link Loader takes all of your components, adds to them others from standard libraries, and creates a complete executable program.
Notes:
a) You'll usually run a whole series of preprocessors and compiles before running a single load, as most programs comprise many source files. Example:
gcc -c tom.c
gcc -c dick.c
gcc -c harry.c
gcc -o tom tom.o dick.o harry.o
In this example, each of three source files is compiled into an object file, and those object files are them joined into an executable file. The
-c options is "compile only", and the
-o option requests an output file of a none-default name.
b) If you fail to give a
-o options on the
gcc run that generates a program file, you'll produce a program file called
a.out
c) You can do a compile and load all at once:
gcc -o tom tom.c dick.c harry.c
but as your program gets larger, that gets to be more and more repeated compile work when you'll only be changing one or two out of (perhaps) hundreds of source files at a time
d) You can use the
make utility to store all your
gcc - and other - commands used to build a program, and by noting down in that file a series of dependencies you can
• ensure that only the phases that need re-doing are done
• store the sometimes-complex gcc instructions so you don't have to keep retyping them.
(written 2008-06-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C201 - C and C based languages - C Language Fundamentals [888] Turning C from source to a running program - (2006-10-06)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2842] Staring a C course with Hello World - why? - (2010-06-30)
[3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
[3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3591] Integer types, and integer overflows, in C - (2012-01-25)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
[4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
A168 - Web Application Deployment - Compiler and development tools [694] Ant and Make - (2006-04-22)
[2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
[3053] Make - automating the commands for building and installing - (2010-11-16)
[3632] What is Make? - (2012-03-02)
[3651] Makefile - some basics, and a demonstration - (2012-03-13)
[3652] A Complete makefile example - (2012-03-14)
[3658] Using Make for a distribution - (2012-03-17)
[3666] Makefile variables - defined internally, from the command line and from the environment - (2012-03-22)
[4013] Web Frameworks - nested templates - (2013-02-22)
[4585] What is make? What is gcc? - (2015-11-28)
Some other Articles
Comparing Objects in C++What a lot of files! (C++ / Polymorphism demo)Spam Filters ... are working!The Composting Cone ChallengeCompiling C programs with gcc - an overviewDynamic Memory Allocation in CWhat are Unions (C programming)Talk on TransWilts train service to Green PartyChecking server performance for PHP generated pagesSlow boot and terminal start on Linux boxes