An Introduction To GCC
An Introduction To GCC
GCC originally stood for GNU C compiler and the original author is
Richard Stallman , founder GNU project
Released first in 1987 – first portable ANSI-C optimizing compiler ,
released as free software
1992 first major revision with 2.0 series – C++ compilation feature added.
Version 3.0 released in 2001.
Support for additional languages like FORTRAN, ADA, Java and
Objective C.
With this GCC now stands for GNU Compiler Collections .
Implementation now guided by a committee
-----------------------------------------------------------------------------------------------
Source: http://blog.pixnet.net/nixchun/post/12331954
_______________________________________________________________
_
• By default, gcc compiles the source code, assembles the assembly language code the compiler
produces, and invokes the UNIX loader, Id, to produce an executable file.
•
• Preprocessing: This step resolves directives like #define, #include, and #if. Like many UNIX
systems, gcc invokes a separate utility called cpp to do the preprocessing.
• Compilation: This produces assembly language from the input files; since the assembler is usually
invoked right away, the output is not normally saved in files.
• Assembly: This takes the assembly language as input and produces object files with extensions.
While some compilers build in the assembly capability, gcc does it by internally invoking a separate
utility called the assembler, gas. GNU assemblers aren't available for all architectures; if the GNU
assembler isn't available, gcc invokes the "native" assembler (as).
• Linking: This is the final stage, where the modules are placed in their proper places in the
executable file. Library functions that the program refers to are also placed in the file. (Systems with
shared libraries use a slightly more complicated method.)
UNIX compilers perform this phase by internally invoking the linker, which is called ld.
• gcc also cleans up by deleting any object files that it created from source files (but not any pre-
existing object files that you specified on the command line).
• ld stands for "link editor" initially, but we haven't heard anybody use this term for years. In some
early UNIX documentation, ld is also called a "loader," which can be confusing because most
people think of loading as reading the executable file into memory at run time.
• General options:
• UNIX linkers search libraries in the order in which they occur on the command line and only resolve
the references that are outstanding at the time the library is searched.
• How gcc handles files:
preprocessing -E
compilation -S
assembly -c
• The GNU version of make: gmake, gnumake.
• The ANSI C standard and "traditional" (Kernighan and Ritchie) C both accept prototypes and define
the behavior of the preprocessor-either explicitly or implicitly. Therefore, the options (-traditional,
-ansi) affect both cpp and gcc.
-traditional Supports the traditional C language, including lots of questionable, but common, practices.
The traditional option also supports all of the FSF's extensions to the C language.
-ansi Supports the ANSI C standard, though somewhat loosely. The FSF's extensions are
recognized, except for a few that are incompatible with the ANSI standard. Thus, ANSI
programs compile correctly, but the compiler doesn't try too hard to reject non-conformant
programs, or programs using non-ANSI features.
-pedantic Issues all the warning messages that are required by the ANSI C standard. Forbids the
use of all the FSF extensions to the C language and considers the use of such extensions
errors. Use __extension__ before the expression to avoid warnings for GNU c extensions.
• Preprocessor options:
-M Read the source files, figure out which other files they include, and output lists of dependencies for
make. The dependency lists are sent to standard output, and compilation doesn't proceed past
preprocessing (i.e., -M implies -E).
-C The preprocessor normally deletes all comments from the program. With -C, it doesn't. The -C
option doesn't automatically imply -E, but gcc won't let you use –C on the command line unless -E
is also present.
• Options to specify libraries:
-nostartfiles Don't use the standard system startup files when linking. This is useful for cross-
compilation, or for compiling code for an embedded processor, where you may want to
provide your own startup file.
-nostdlib Don't use the standard libraries and startup files when linking.
-static Link only to static libraries, not shared libraries.
-shared If shared libraries are available, use them wherever possible, rather than static libraries.
(This is the default.)
• Debugging and profiling options:
These options request the compiler to create additional code and an expanded symbol table for the
various profilers and debuggers (dbx, prof, gprof, and the branch count profiler).
-p
Link the program for profiling with prof. When you execute a program compiled with this option, it
produces a file named mon.out that contains program execution statistics. The profiler prof reads
this file and produces a table describing your program's execution.
-pg Link the program for profiling with gprof. Executing a program compiled with this option produces a
file named gmon.out that includes execution statistics. The profiler gprof reads this file and
produces detailed information about your program's execution.
-g Generate an expanded symbol table for debugging, the additional symbols tell the assembler
where to find the source code. If you need to use your system's native debugger, and you have
trouble with -g, try -gstabs for dbx on BSD UNIX systems; -gcoff for sdb under SVR3 and earlier
releases of System V; -gxcoff for dbx on the RS/600a0n;d -gdwarf for sVR4 debuggers.
• Optimization:
-Wa,option- Pass the option-list to the assembler. Ex: gcc –c –g –Wa,-alh,-L source.c, producing a
list listing of the assembly language generated, together with C source listings, -L stands
for retain local labels.
-Wl,option- Pass the option-list to the linker.
list
• as list-of-options list-of-source-files
The list-of source-files can contain the special name --, which means “Read standard input for
assembly source code”.
Generate a listing of the high-level code only. (Only possible if the object file was compiled with
-ah
gcc's -g option).
-o name Instead of naming the executable output file a.out, name it name.
-Ilibname Link the program to the library named libname.a. The linker looks in the directories /lib
and /usr/Iib to find this library.
-Llibpath To find any libraries, look in the directory dir before looking in the standard library
directories /lib and /usr/lib.
-s Remove the symbol table from the executable output file. Using the program strip has
the same effect.
-x Remove all local symbols from the output file. Global symbols (subprograms and
common block names) remain in the output file. Ignored unless -s is specified.
-n Make the text segment read-only.
-r Create an object file that can be included in further linking runs (i.e., further passes
through ld). Among other things, this inhibits "Undefined symbol" messages and
prevents ld from creating common storage immediately. If you wish to create common
storage at this time, use the -d option also.
-e name Use the symbol name (address, symbol offset+section_name) as the entry point to the
executable program. By default, the entry point is the beginning of the first object
module. gcc automatically links your object files with a run-time initialization module
(/usr/lib/crt0.o that starts your program and provides its initial entry point. If you run the
linker separately, you must either put /usr/lib/crt0.o at the start of your object files, or
provide your own entry point.
-M Produce a "load map" that shows where each function is located in the resulting object
file, where each section begins and ends, and the value of each global symbol.
-b format Read object modules in the given format. To get a list of formats that Id understands,
give the command objdump -i.
-oformat Create object modules in the given format.
format
• Link-order optimization: By changing the link order, you're changing the way the executable file
"lies" in the instruction cache.
• ar rs lib-name list-of-files /* Create a new library */
The option r indicates that the command ar should add the files named in the list-of-files to the
library named lib-name, creating a new library if necessary. If a file is mentioned twice in the list-of-
files, ar includes it in the archive twice. The s option tells ar to produce an index for the archive; this
is the function that ranlib would perform. If you include the s option whenever you create or modify
a library, you'll never need to use ranlib.
• ar rus lib-name list-of-files /* Update a library */
This compares the dates of any listed files with the version of the file in the library. If the file in list-
files is more recent than the version contained in the library, ar substitutes the newer version for the
older version.
• ar ds lib-name list-of-files /* Delete one or more files form a library */
This deletes all the files found in list-of-files.
ar x lib-name list-of-files /* Extract one or more files form a library */
This does not modify the library file itself. It extracts the files named in the list-of-files from the
library, regenerating them in the current directory with their original names. Normally, the
timestamp of the extracted files is the time at which ar recreated them. If you use the option xo
instead of x, ar sets the timestamp of the extracted files to the time recorded in the archive.
• Cross-compilation needs: cross-compiler, assembler, linker, header files, libraries, and run-time
startup files (crt0.o for UNIX system) for the target architecture.
End of article