An Introduction To GCC
An Introduction To GCC
Brian Gough
Foreword by Richard M. Stallman
Copyright c 2004, 2005 Network Theory Ltd.
Permission is granted to copy, distribute and/or modify this document under the terms of
the GNU Free Documentation License, Version 1.2 or any later version published by the
Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being “A
Network Theory Manual”, and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled “GNU Free Documentation License”.
(a) The Back-Cover Text is: “The development of this manual was funded entirely by
Network Theory Ltd. Copies published by Network Theory Ltd raise money for more free
documentation.”
i
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1 A brief history of GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Major features of GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Programming in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Conventions used in this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Compiling a C program . . . . . . . . . . . . . . . . . . . . 5
2.1 Compiling a simple C program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Finding errors in a simple program . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Compiling multiple source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Compiling files independently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4.1 Creating object files from source files . . . . . . . . . . . . . . . . . . . . . 8
2.4.2 Creating executables from object files . . . . . . . . . . . . . . . . . . . . 9
2.5 Recompiling and relinking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.6 A simple makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.7 Linking with external libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7.1 Link order of libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.8 Using library header files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Compilation options . . . . . . . . . . . . . . . . . . . . . . 15
3.1 Setting search paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.1 Search path example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.2 Environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.3 Extended search paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2 Shared libraries and static libraries . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3 C language standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.1 ANSI/ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.2 Strict ANSI/ISO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.3 Selecting specific standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4 Warning options in -Wall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.5 Additional warning options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.6 Recommended warning options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8 Platform-specific options . . . . . . . . . . . . . . . . . . 54
8.1 Intel and AMD x86 options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.1.1 x86 extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
8.1.2 x86 64-bit processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.2 DEC Alpha options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.3 SPARC options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.4 POWER/PowerPC options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
8.5 Multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.6 Floating-point issues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.7 Portability of signed and unsigned types . . . . . . . . . . . . . . . . . . . . . 59
iii
9 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.1 Help for command-line options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.2 Version numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.3 Verbose compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.4 Stopping a program in an infinite loop . . . . . . . . . . . . . . . . . . . . . . . 63
9.5 Preventing excessive memory usage . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10 Compiler-related tools . . . . . . . . . . . . . . . . . . . 66
10.1 Creating a library with the GNU archiver . . . . . . . . . . . . . . . . . . . 66
10.2 Using the profiler gprof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
10.3 Coverage testing with gcov . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
14 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
1
Foreword
This foreword has been kindly contributed by Richard M. Stallman, the principal author of
GCC and founder of the GNU Project.
This book is a guide to getting started with GCC, the GNU Compiler Collection. It will
tell you how to use GCC as a programming tool. GCC is a programming tool, that’s true—
but it is also something more. It is part of a 20-year campaign for freedom for computer
users.
We all want good software, but what does it mean for software to be “good”? Convenient
features and reliability are what it means to be technically good, but that is not enough.
Good software must also be ethically good: it has to respect the users’ freedom.
As a user of software, you should have the right to run it as you see fit, the right to
study the source code and then change it as you see fit, the right to redistribute copies of it
to others, and the right to publish a modified version so that you can contribute to building
the community. When a program respects your freedom in this way, we call it free software.
Before GCC, there were other compilers for C, Fortran, Ada, etc. But they were not free
software; you could not use them in freedom. I wrote GCC so we could use a compiler
without giving up our freedom.
A compiler alone is not enough—to use a computer system, you need a whole operating
system. In 1983, all operating systems for modern computers were non-free. To remedy
this, in 1984 I began developing the GNU operating system, a Unix-like system that would
be free software. Developing GCC was one part of developing GNU.
By the early 90s, the nearly-finished GNU operating system was completed by the ad-
dition of a kernel, Linux, that became free software in 1992. The combined GNU/Linux
operating system has achieved the goal of making it possible to use a computer in freedom.
But freedom is never automatically secure, and we need to work to defend it. The Free
Software Movement needs your support.
Richard M. Stallman
February 2004
Chapter 1: Introduction 2
1 Introduction
The purpose of this book is to explain the use of the GNU C and C++ compilers, gcc and
g++. After reading this book you should understand how to compile a program, and how to
use basic compiler options for optimization and debugging. This book does not attempt to
teach the C or C++ languages themselves, since this material can be found in many other
places (see [Further reading], page 89).
Experienced programmers who are familiar with other systems, but new to the GNU
compilers, can skip the early sections of the chapters “Compiling a C program”, “Using
the preprocessor” and “Compiling a C++ program”. The remaining sections and chapters
should provide a good overview of the features of GCC for those already know how to use
other compilers.
written in C with a strong focus on portability, and can compile itself, so it can be adapted
to new systems easily.
GCC has multiple language frontends, for parsing different languages. Programs in each
language can be compiled, or cross-compiled, for any architecture. For example, an ADA
program can be compiled for a microcontroller, or a C program for a supercomputer.
GCC has a modular design, allowing support for new languages and architectures to be
added. Adding a new language front-end to GCC enables the use of that language on any
architecture, provided that the necessary run-time facilities (such as libraries) are available.
Similarly, adding support for a new architecture makes it available to all languages.
Finally, and most importantly, GCC is free software, distributed under the GNU General
Public License (GNU GPL).1 This means you have the freedom to use and to modify GCC,
as with all GNU software. If you need support for a new type of CPU, a new language, or
a new feature you can add it yourself, or hire someone to enhance GCC for you. You can
hire someone to fix a bug if it is important for your work.
Furthermore, you have the freedom to share any enhancements you make to GCC. As a
result of this freedom you can also make use of enhancements to GCC developed by others.
The many features offered by GCC today show how this freedom to cooperate works to
benefit you, and everyone else who uses GCC.
When a command in an example is too long to fit in a single line it is wrapped and then
indented on subsequent lines, like this:
$ echo "an example of a line which is too long to fit
in this manual"
When entered at the keyboard, the entire command should be typed on a single line.
The example source files used in this manual can be downloaded from the publisher’s
website,2 or entered by hand using any text editor, such as the standard GNU editor, emacs.
The example compilation commands use gcc and g++ as the names of the GNU C and C++
compilers, and cc to refer to other compilers. The example programs should work with any
version of GCC. Any command-line options which are only available in recent versions of
GCC are noted in the text.
The examples assume the use of a GNU operating system—there may be minor differ-
ences in the output on other systems. Some non-essential and verbose system-dependent
output messages (such as very long system paths) have been edited in the examples for
brevity. The commands for setting environment variables use the syntax of the standard
GNU shell (bash), and should work with any version of the Bourne shell.
2
See http://www.network-theory.co.uk/gcc/intro/
Chapter 2: Compiling a C program 5
2 Compiling a C program
This chapter describes how to compile C programs using gcc. Programs can be compiled
from a single source file or from multiple source files, and may use system libraries and
header files.
Compilation refers to the process of converting a program from the textual source code,
in a programming language such as C or C++, into machine code, the sequence of 1’s and
0’s used to control the central processing unit (CPU) of the computer. This machine code
is then stored in a file known as an executable file, sometimes referred to as a binary file.
int
main (void)
{
printf ("Hello, world!\n");
return 0;
}
We will assume that the source code is stored in a file called ‘hello.c’. To compile the file
‘hello.c’ with gcc, use the following command:
$ gcc -Wall hello.c -o hello
This compiles the source code in ‘hello.c’ to machine code and stores it in an executable
file ‘hello’. The output file for the machine code is specified using the ‘-o’ option. This
option is usually given as the last argument on the command line. If it is omitted, the
output is written to a default file called ‘a.out’.
Note that if a file with the same name as the executable file already exists in the current
directory it will be overwritten.
The option ‘-Wall’ turns on all the most commonly-used compiler warnings—it is rec-
ommended that you always use this option! There are many other warning options which
will be discussed in later chapters, but ‘-Wall’ is the most important. GCC will not produce
any warnings unless they are enabled. Compiler warnings are an essential aid in detecting
problems when programming in C and C++.
In this case, the compiler does not produce any warnings with the ‘-Wall’ option, since
the program is completely valid. Source code which does not produce any warnings is said
to compile cleanly.
To run the program, type the path name of the executable like this:
$ ./hello
Hello, world!
This loads the executable file into memory and causes the CPU to begin executing the
instructions contained within it. The path ./ refers to the current directory, so ./hello
loads and runs the executable file ‘hello’ located in the current directory.
Chapter 2: Compiling a C program 6
int
main (void)
{
printf ("Two plus two is %f\n", 4);
return 0;
}
This error is not obvious at first sight, but can be detected by the compiler if the warning
option ‘-Wall’ has been enabled.
Compiling the program above, ‘bad.c’, with the warning option ‘-Wall’ produces the
following message:
$ gcc -Wall bad.c -o bad
bad.c: In function ‘main’:
bad.c:6: warning: double format, different
type arg (arg 2)
This indicates that a format string has been used incorrectly in the file ‘bad.c’ at line 6. The
messages produced by GCC always have the form file:line-number:message. The compiler
distinguishes between error messages, which prevent successful compilation, and warning
messages which indicate possible problems (but do not stop the program from compiling).
In this case, the correct format specifier should be ‘%d’ for an integer argument. The
allowed format specifiers for printf can be found in any general book on C, such as the
GNU C Library Reference Manual (see [Further reading], page 89).
Without the warning option ‘-Wall’ the program appears to compile cleanly, but pro-
duces incorrect results:
$ gcc bad.c -o bad
$ ./bad
Two plus two is 2.585495 (incorrect output)
The incorrect format specifier causes the output to be corrupted, because the function
printf is passed an integer instead of a floating-point number. Integers and floating-point
numbers are stored in different formats in memory, and generally occupy different numbers
of bytes, leading to a spurious result. The actual output shown above may differ, depending
on the specific platform and environment.
Clearly, it is very dangerous to develop a program without checking for compiler warn-
ings. If there are any functions which are not used correctly they can cause the program
to crash or produce incorrect results. Turning on the compiler warning option ‘-Wall’ will
catch many of the commonest errors which occur in C programming.
Chapter 2: Compiling a C program 7
int
main (void)
{
hello ("world");
return 0;
}
The original call to the printf system function in the previous program ‘hello.c’ has been
replaced by a call to a new external function hello, which we will define in a separate file
‘hello_fn.c’.
The main program also includes the header file ‘hello.h’ which will contain the dec-
laration of the function hello. The declaration is used to ensure that the types of the
arguments and return value match up correctly between the function call and the function
definition. We no longer need to include the system header file ‘stdio.h’ in ‘main.c’ to
declare the function printf, since the file ‘main.c’ does not call printf directly.
The declaration in ‘hello.h’ is a single line specifying the prototype of the function
hello:
void hello (const char * name);
The definition of the function hello itself is contained in the file ‘hello_fn.c’:
#include <stdio.h>
#include "hello.h"
void
hello (const char * name)
{
printf ("Hello, %s!\n", name);
}
This function prints the message “Hello, name!” using its argument as the value of name.
Incidentally, the difference between the two forms of the include statement #include
"FILE.h" and #include <FILE.h> is that the former searches for ‘FILE.h’ in the current di-
rectory before looking in the system header file directories. The include statement #include
<FILE.h> searches the system header files, but does not look in the current directory by
default.
To compile these source files with gcc, use the following command:
$ gcc -Wall main.c hello_fn.c -o newhello
In this case, we use the ‘-o’ option to specify a different output file for the executable,
‘newhello’. Note that the header file ‘hello.h’ is not specified in the list of files on the
Chapter 2: Compiling a C program 8
command line. The directive #include "hello.h" in the source files instructs the compiler
to include it automatically at the appropriate points.
To run the program, type the path name of the executable:
$ ./newhello
Hello, world!
All the parts of the program have been combined into a single executable file, which produces
the same result as the executable created from the single source file used earlier.
int
main (void)
{
hello ("everyone"); /* changed from "world" */
return 0;
}
The updated file ‘main.c’ can now be recompiled with the following command:
$ gcc -Wall -c main.c
This produces a new object file ‘main.o’. There is no need to create a new object file for
‘hello_fn.c’, since that file and the related files that it depends on, such as header files,
have not changed.
The new object file can be relinked with the hello function to create a new executable
file:
$ gcc main.o hello_fn.o -o hello
The resulting executable ‘hello’ now uses the new main function to produce the following
output:
$ ./hello
Hello, everyone!
Chapter 2: Compiling a C program 10
Note that only the file ‘main.c’ has been recompiled, and then relinked with the existing
object file for the hello function. If the file ‘hello_fn.c’ had been modified instead, we
could have recompiled ‘hello_fn.c’ to create a new object file ‘hello_fn.o’ and relinked
this with the existing file ‘main.o’.1
In a large project with many source files, recompiling only those that have been modified
can make a significant saving. The process of recompiling only the modified files in a project
can be automated with the standard Unix program make.
clean:
rm -f main main.o hello_fn.o
The file can be read like this: using the C compiler gcc, with compilation option ‘-Wall’,
build the target executable main from the object files ‘main.o’ and ‘hello_fn.o’ (these, in
turn, will be built via implicit rules from ‘main.c’ and ‘hello_fn.c’). The target clean
1
If the prototype of a function has changed, it is necessary to modify and recompile all of the other source
files which use it.
Chapter 2: Compiling a C program 11
has no dependencies and simply removes all the compiled files.2 The option ‘-f’ (force) on
the rm command suppresses any error messages if the files do not exist.
To use the makefile, type make. When called with no arguments, the first target in the
makefile is built, producing the executable ‘main’:
$ make
gcc -Wall -c -o main.o main.c
gcc -Wall -c -o hello_fn.o hello_fn.c
gcc main.o hello_fn.o -o main
$ ./main
Hello, world!
To rebuild the executable after modifying a source file, simply type make again. By checking
the timestamps of the target and dependency files, make identifies the files which have
changed and regenerates the corresponding intermediate files needed to update the targets:
$ emacs main.c (edit the file)
$ make
gcc -Wall -c -o main.o main.c
gcc main.o hello_fn.o -o main
$ ./main
Hello, everyone!
Finally, to remove the generated files, type make clean:
$ make clean
rm -f main main.o hello_fn.o
A more sophisticated makefile would usually contain additional targets for installation (make
install) and testing (make check).
The examples in the rest of this book are small enough not to need makefiles, but the
use of make is recommended for any larger programs.
given in the header file ‘/usr/include/math.h’. The C standard library itself is stored in
‘/usr/lib/libc.a’ and contains functions specified in the ANSI/ISO C standard, such as
‘printf’—this library is linked by default for every C program.
Here is an example program which makes a call to the external function sqrt in the
math library ‘libm.a’:
#include <math.h>
#include <stdio.h>
int
main (void)
{
double x = 2.0;
double y = sqrt (x);
printf ("The square root of %f is %f\n", x, y);
return 0;
}
Trying to create an executable from this source file alone causes the compiler to give an
error at the link stage:
$ gcc -Wall calc.c -o calc
/tmp/ccbR6Ojm.o: In function ‘main’:
/tmp/ccbR6Ojm.o(.text+0x19): undefined reference
to ‘sqrt’
The problem is that the reference to the sqrt function cannot be resolved without the
external math library ‘libm.a’. The function sqrt is not defined in the program or the
default library ‘libc.a’, and the compiler does not link to the file ‘libm.a’ unless it is
explicitly selected. Incidentally, the file mentioned in the error message ‘/tmp/ccbR60jm.o’
is a temporary object file created by the compiler from ‘calc.c’, in order to carry out the
linking process.
To enable the compiler to link the sqrt function to the main program ‘calc.c’ we need
to supply the library ‘libm.a’. One obvious but cumbersome way to do this is to specify it
explicitly on the command line:
$ gcc -Wall calc.c /usr/lib/libm.a -o calc
The library ‘libm.a’ contains object files for all the mathematical functions, such as sin,
cos, exp, log and sqrt. The linker searches through these to find the object file containing
the sqrt function.
Once the object file for the sqrt function has been found, the main program can be
linked and a complete executable produced:
$ ./calc
The square root of 2.0 is 1.414214
The executable file includes the machine code for the main function and the machine code
for the sqrt function, copied from the corresponding object file in the library ‘libm.a’.
To avoid the need to specify long paths on the command line, the compiler provides a
short-cut option ‘-l’ for linking against libraries. For example, the following command,
Chapter 2: Compiling a C program 13
int
Chapter 2: Compiling a C program 14
main (void)
{
double x = pow (2.0, 3.0);
printf ("Two cubed is %f\n", x);
return 0;
}
However, the program contains an error—the #include statement for ‘math.h’ is missing,
so the prototype double pow (double x, double y) given there will not be seen by the
compiler.
Compiling the program without any warning options will produce an executable file
which gives incorrect results:
$ gcc badpow.c -lm
$ ./a.out
Two cubed is 2.851120 (incorrect result, should be 8)
The results are corrupted because the arguments and return value of the call to pow are
passed with incorrect types.4 This can be detected by turning on the warning option ‘-Wall’:
$ gcc -Wall badpow.c -lm
badpow.c: In function ‘main’:
badpow.c:6: warning: implicit declaration of
function ‘pow’
This example shows again the importance of using the warning option ‘-Wall’ to detect
serious problems that could otherwise easily be overlooked.
4
The actual output shown above may differ, depending on the specific platform and environment.
Chapter 3: Compilation options 15
3 Compilation options
This chapter describes other commonly-used compiler options available in GCC. These
options control features such as the search paths used for locating libraries and include
files, the use of additional warnings and diagnostics, preprocessor macros and C language
dialects.
#include <stdio.h>
#include <gdbm.h>
int
main (void)
{
GDBM_FILE dbf;
datum key = { "testkey", 7 }; /* key, length */
datum value = { "testvalue", 9 }; /* value, length */
This produces the final executable linked to the GDBM library. Before seeing how to run
this executable we will take a brief look at the environment variables that affect the ‘-I’
and ‘-L’ options.
Note that you should never place the absolute paths of header files in #include state-
ments in your source code, as this will prevent the program from compiling on other systems.
The ‘-I’ option or the INCLUDE_PATH variable described below should always be used to set
the include path for header files.
2
In GNU Bash, the shorter form export VARIABLE =VALUE is also allowed.
Chapter 3: Compilation options 18
library, the machine code from the object files for any external functions used by the program
is copied from the library into the final executable.
Shared libraries are handled with a more advanced form of linking, which makes the
executable file smaller. They use the extension ‘.so’, which stands for shared object.
An executable file linked against a shared library contains only a small table of the
functions it requires, instead of the complete machine code from the object files for the
external functions. Before the executable file starts running, the machine code for the
external functions is copied into memory from the shared library file on disk by the operating
system—a process referred to as dynamic linking.
Dynamic linking makes executable files smaller and saves disk space, because one copy
of a library can be shared between multiple programs. Most operating systems also provide
a virtual memory mechanism which allows one copy of a shared library in physical memory
to be used by all running programs, saving memory as well as disk space.
Furthermore, shared libraries make it possible to update a library without recompiling
the programs which use it (provided the interface to the library does not change).
Because of these advantages gcc compiles programs to use shared libraries by default
on most systems, if they are available. Whenever a static library ‘libNAME.a’ would be
used for linking with the option ‘-lNAME ’ the compiler first checks for an alternative shared
library with the same name and a ‘.so’ extension.
In this case, when the compiler searches for the ‘libgdbm’ library in the link path, it
finds the following two files in the directory ‘/opt/gdbm-1.8.3/lib’:
$ cd /opt/gdbm-1.8.3/lib
$ ls libgdbm.*
libgdbm.a libgdbm.so
Consequently, the ‘libgdbm.so’ shared object file is used in preference to the ‘libgdbm.a’
static library.
However, when the executable file is started its loader function must find the shared
library in order to load it into memory. By default the loader searches for shared libraries
only in a predefined set of system directories, such as ‘/usr/local/lib’ and ‘/usr/lib’. If
the library is not located in one of these directories it must be added to the load path.4
The simplest way to set the load path is through the environment variable LD_LIBRARY_
PATH. For example, the following commands set the load path to ‘/opt/gdbm-1.8.3/lib’
so that ‘libgdbm.so’ can be found:
$ LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib
$ export LD_LIBRARY_PATH
$ ./a.out
Storing key-value pair... done.
The executable now runs successfully, prints its message and creates a DBM file called
‘test’ containing the key-value pair ‘testkey’ and ‘testvalue’.
4
Note that the directory containing the shared library can, in principle, be stored (“hard-coded”) in the
executable itself using the linker option ‘-rpath’, but this is not usually done since it creates problems
if the library is moved or the executable is copied to another system.
Chapter 3: Compilation options 20
To save typing, the LD_LIBRARY_PATH environment variable can be set automatically for
each session using the appropriate login file, such as ‘.bash_profile’ for the GNU Bash
shell.
Several shared library directories can be placed in the load path, as a colon separated
list DIR1 :DIR2 :DIR3 :...:DIRN . For example, the following command sets the load path
to use the ‘lib’ directories under ‘/opt/gdbm-1.8.3’ and ‘/opt/gtk-1.4’:
$ LD_LIBRARY_PATH=/opt/gdbm-1.8.3/lib:/opt/gtk-1.4/lib
$ export LD_LIBRARY_PATH
If the load path contains existing entries, it can be extended using the syntax LD_LIBRARY_
PATH=NEWDIRS :$LD_LIBRARY_PATH. For example, the following command adds the direc-
tory ‘/opt/gsl-1.5/lib’ to the load path shown above:
$ LD_LIBRARY_PATH=/opt/gsl-1.5/lib:$LD_LIBRARY_PATH
$ echo $LD_LIBRARY_PATH
/opt/gsl-1.5/lib:/opt/gdbm-1.8.3/lib:/opt/gtk-1.4/lib
It is possible for the system administrator to set the LD_LIBRARY_PATH variable for all
users, by adding it to a default login script, such as ‘/etc/profile’. On GNU systems, a
system-wide path can also be defined in the loader configuration file ‘/etc/ld.so.conf’.
Alternatively, static linking can be forced with the ‘-static’ option to gcc to avoid the
use of shared libraries:
$ gcc -Wall -static -I/opt/gdbm-1.8.3/include/
-L/opt/gdbm-1.8.3/lib/ dbmain.c -lgdbm
This creates an executable linked with the static library ‘libgdbm.a’ which can be run
without setting the environment variable LD_LIBRARY_PATH or putting shared libraries in
the default directories:
$ ./a.out
Storing key-value pair... done.
As noted earlier, it is also possible to link directly with individual library files by specifying
the full path to the library on the command line. For example, the following command will
link directly with the static library ‘libgdbm.a’,
$ gcc -Wall -I/opt/gdbm-1.8.3/include
dbmain.c /opt/gdbm-1.8.3/lib/libgdbm.a
and the command below will link with the shared library file ‘libgdbm.so’:
$ gcc -Wall -I/opt/gdbm-1.8.3/include
dbmain.c /opt/gdbm-1.8.3/lib/libgdbm.so
In the latter case it is still necessary to set the library load path when running the executable.
3.3.1 ANSI/ISO
Occasionally a valid ANSI/ISO program may be incompatible with the extensions in GNU
C. To deal with this situation, the compiler option ‘-ansi’ disables those GNU extensions
which are in conflict with the ANSI/ISO standard. On systems using the GNU C Library
(glibc) it also disables extensions to the C standard library. This allows programs written
for ANSI/ISO C to be compiled without any unwanted effects from GNU extensions.
For example, here is a valid ANSI/ISO C program which uses a variable called asm:
#include <stdio.h>
int
main (void)
{
const char asm[] = "6502";
printf ("the string asm is ’%s’\n", asm);
return 0;
}
The variable name asm is valid under the ANSI/ISO standard, but this program will not
compile in GNU C because asm is a GNU C keyword extension (it allows native assembly
instructions to be used in C functions). Consequently, it cannot be used as a variable name
without giving a compilation error:
$ gcc -Wall ansi.c
ansi.c: In function ‘main’:
ansi.c:6: parse error before ‘asm’
ansi.c:7: parse error before ‘asm’
In contrast, using the ‘-ansi’ option disables the asm keyword extension, and allows the
program above to be compiled correctly:
$ gcc -Wall -ansi ansi.c
$ ./a.out
the string asm is ’6502’
For reference, the non-standard keywords and macros defined by the GNU C extensions
are asm, inline, typeof, unix and vax. More details can be found in the GCC Reference
Manual “Using GCC” (see [Further reading], page 89).
The next example shows the effect of the ‘-ansi’ option on systems using the GNU
C Library, such as GNU/Linux systems. The program below prints the value of pi, π =
3.14159..., from the preprocessor definition M_PI in the header file ‘math.h’:
#include <math.h>
#include <stdio.h>
int
main (void)
{
printf ("the value of pi is %f\n", M_PI);
return 0;
}
Chapter 3: Compilation options 22
The constant M_PI is not part of the ANSI/ISO C standard library (it comes from the BSD
version of Unix). In this case, the program will not compile with the ‘-ansi’ option:
$ gcc -Wall -ansi pi.c
pi.c: In function ‘main’:
pi.c:7: ‘M_PI’ undeclared (first use in this function)
pi.c:7: (Each undeclared identifier is reported only once
pi.c:7: for each function it appears in.)
The program can be compiled without the ‘-ansi’ option. In this case both the language
and library extensions are enabled by default:
$ gcc -Wall pi.c
$ ./a.out
the value of pi is 3.141593
It is also possible to compile the program using ANSI/ISO C, by enabling only the extensions
in the GNU C Library itself. This can be achieved by defining special macros, such as _
GNU_SOURCE, which enable extensions in the GNU C Library:5
$ gcc -Wall -ansi -D_GNU_SOURCE pi.c
$ ./a.out
the value of pi is 3.141593
The GNU C Library provides a number of these macros (referred to as feature test
macros) which allow control over the support for POSIX extensions (_POSIX_C_SOURCE),
BSD extensions (_BSD_SOURCE), SVID extensions (_SVID_SOURCE), XOPEN extensions
(_XOPEN_SOURCE) and GNU extensions (_GNU_SOURCE).
The _GNU_SOURCE macro enables all the extensions together, with the POSIX extensions
taking precedence over the others in cases where they conflict. Further information about
feature test macros can be found in the GNU C Library Reference Manual (see [Further
reading], page 89).
return 0;
5
The ‘-D’ option for defining macros will be explained in detail in the next chapter.
Chapter 3: Compilation options 23
}
This program will compile with ‘-ansi’, because support for variable length arrays does not
interfere with the compilation of valid ANSI/ISO programs—it is a backwards-compatible
extension:
$ gcc -Wall -ansi gnuarray.c
However, compiling with ‘-ansi -pedantic’ reports warnings about violations of the
ANSI/ISO standard:
$ gcc -Wall -ansi -pedantic gnuarray.c
gnuarray.c: In function ‘main’:
gnuarray.c:5: warning: ISO C90 forbids variable-size
array ‘x’
Note that an absence of warnings from ‘-ansi -pedantic’ does not guarantee that a pro-
gram strictly conforms to the ANSI/ISO standard. The standard itself specifies only a
limited set of circumstances that should generate diagnostics, and these are what ‘-ansi
-pedantic’ reports.
int
main (void)
{
printf ("hello world\n");
return;
}
The lack of a return value in the code above could be the result of an acci-
dental omission by the programmer—the value returned by the main function
is actually the return value of the printf function (the number of characters
printed). To avoid ambiguity, it is preferable to use an explicit value in the
return statement, either as a variable or a constant, such as return 0.
The complete set of warning options included in ‘-Wall’ can be found in the GCC
Reference Manual “Using GCC” (see [Further reading], page 89). The options included in
‘-Wall’ have the common characteristic that they report constructions which are always
wrong, or can easily be rewritten in an unambiguously correct way. This is why they are
so useful—any warning produced by ‘-Wall’ can be taken as an indication of a potentially
serious problem.
Chapter 3: Compilation options 25
int
Chapter 3: Compilation options 26
main (void)
{
double x = -3.14;
double y = abs(x); /* should be fabs(x) */
printf ("x = %g |x| = %g\n", x, y);
return 0;
}
Compiling this function with ‘-Wall’ does not produce a warning,
$ gcc -Wall wabs.c
$ ./a.out
x = -3.14 |x| = 3 (incorrect)
but does give a warning with ‘-Wconversion’:
gcc -Wall -Wconversion wabs.c
wabs.c: In function ‘main’:
wabs.c:8: warning: passing arg 1 of ‘abs’ as
integer rather than floating due to prototype
The ‘-Wconversion’ option also catches errors such as the assignment of a
negative value to an unsigned variable, as in the following code,
unsigned int x = -1;
This is technically allowed by the ANSI/ISO C standard (with the negative
integer being converted to a positive integer, according to the machine repre-
sentation) but could be a simple programming error. If you need to perform
such a conversion you can use an explicit cast, such as (unsigned int)-1, to
avoid any warnings from this option. On two’s-complement machines the cast of
−1 gives the maximum number that can be represented by an unsigned integer.
‘-Wshadow’
This option warns about the redeclaration of a variable name in a scope where
it has already been declared. This is referred to as variable shadowing, and
causes confusion about which occurrence of the variable corresponds to which
value.
The following function declares a local variable y that shadows the declaration
in the body of the function:
double
test (double x)
{
double y = 1.0;
{
double y;
y = x;
}
return y;
}
This is valid ANSI/ISO C, where the return value is 1. The shadowing of the
variable y might make it seem (incorrectly) that the return value is x, when
looking at the line y = x (especially in a large and complicated function).
Chapter 3: Compilation options 27
Shadowing can also occur for function names. For example, the following pro-
gram attempts to define a variable sin which shadows the standard function
sin(x).
double
sin_series (double x)
{
/* series expansion for small x */
double sin = x * (1.0 - x * x / 6.0);
return sin;
}
This error will be detected by the ‘-Wshadow’ option.
‘-Wcast-qual’
This option warns about pointers that are cast to remove a type qualifier, such
as const. For example, the following function discards the const qualifier from
its input argument, allowing it to be overwritten:
void
f (const char * str)
{
char * s = (char *)str;
s[0] = ’\0’;
}
The modification of the original contents of str is a violation of its const
property. This option will warn about the improper cast of the variable str
which allows the string to be modified.
‘-Wwrite-strings’
This option implicitly gives all string constants defined in the program a const
qualifier, causing a compile-time warning if there is an attempt to overwrite
them. The result of modifying a string constant is not defined by the ANSI/ISO
standard, and the use of writable string constants is deprecated in GCC.
‘-Wtraditional’
This option warns about parts of the code which would be interpreted dif-
ferently by an ANSI/ISO compiler and a “traditional” pre-ANSI compiler.6
When maintaining legacy software it may be necessary to investigate whether
the traditional or ANSI/ISO interpretation was intended in the original code
for warnings generated by this option.
The options above produce diagnostic warning messages, but allow the compilation to
continue and produce an object file or executable. For large programs it can be desirable to
catch all the warnings by stopping the compilation whenever a warning is generated. The
‘-Werror’ option changes the default behavior by converting warnings into errors, stopping
the compilation whenever a warning occurs.
6
The traditional form of the C language was described in the original C reference manual “The C Pro-
gramming Language (First Edition)” by Kernighan and Ritchie.
Chapter 3: Compilation options 28
int
main (void)
{
#ifdef TEST
printf ("Test mode\n");
#endif
printf ("Running...\n");
return 0;
}
When the macro is defined, the preprocessor includes the corresponding code up to the
closing #endif command. In this example, the macro which is tested is called TEST, and
the conditional part of the source code is a printf statement which prints the message
“Test mode”.
The gcc option ‘-DNAME ’ defines a preprocessor macro NAME from the command line. If
the program above is compiled with the command-line option ‘-DTEST’, the macro TEST will
be defined and the resulting executable will print both messages:
$ gcc -Wall -DTEST dtest.c
$ ./a.out
Test mode
Running...
If the same program is compiled without the ‘-D’ option then the “Test mode” message is
omitted from the source code after preprocessing, and the final executable does not include
the code for it:
$ gcc -Wall dtest.c
$ ./a.out
Running...
Macros are generally undefined, unless specified on the command line with the option ‘-D’,
or in a source file (or library header file) with #define. Some macros are automatically
defined by the compiler—these typically use a reserved namespace beginning with a double-
underscore prefix ‘__’.
The complete set of predefined macros can be listed by running the GNU preprocessor
cpp with the option ‘-dM’ on an empty file:
1
In recent versions of GCC the preprocessor is integrated into the compiler, although a separate cpp
command is also provided.
Chapter 4: Using the preprocessor 30
int
main (void)
{
printf ("Value of NUM is %d\n", NUM);
return 0;
}
Note that macros are not expanded inside strings—only the occurrence of NUM outside the
string is substituted by the preprocessor.
To define a macro with a value, the ‘-D’ command-line option can be used in the form
‘-DNAME =VALUE ’. For example, the following command line defines NUM to be 100 when
compiling the program above:
$ gcc -Wall -DNUM=100 dtestval.c
$ ./a.out
Value of NUM is 100
This example uses a number, but a macro can take values of any form. Whatever the value
of the macro is, it is inserted directly into the source code at the point where the macro
name occurs. For example, the following definition expands the occurrences of NUM to 2+2
during preprocessing:
$ gcc -Wall -DNUM="2+2" dtestval.c
$ ./a.out
Value of NUM is 4
After the preprocessor has made the substitution NUM 7→ 2+2 this is equivalent to compiling
the following program:
#include <stdio.h>
Chapter 4: Using the preprocessor 31
int
main (void)
{
printf ("Value of NUM is %d\n", 2+2);
return 0;
}
Note that it is a good idea to surround macros by parentheses whenever they are part of
an expression. For example, the following program uses parentheses to ensure the correct
precedence for the multiplication 10*NUM:
#include <stdio.h>
int
main (void)
{
printf ("Ten times NUM is %d\n", 10 * (NUM));
return 0;
}
With these parentheses, it produces the expected result when compiled with the same
command line as above:
$ gcc -Wall -DNUM="2+2" dtestmul10.c
$ ./a.out
Ten times NUM is 40
Without parentheses, the program would produce the value 22 from the literal form of the
expression 10*2+2 = 22, instead of the desired value 10*(2+2) = 40.
When a macro is defined with ‘-D’ alone, gcc uses a default value of 1. For example,
compiling the original test program with the option ‘-DNUM’ generates an executable which
produces the following output:
$ gcc -Wall -DNUM dtestval.c
$ ./a.out
Value of NUM is 1
A macro can be defined with an empty value using quotes on the command line, -DNAME ="".
Such a macro is still treated as defined by conditionals such as #ifdef, but expands to
nothing.
A macro containing quotes can be defined using shell-escaped quote characters. For ex-
ample, the command-line option -DMESSAGE=’"Hello, World!"’ defines a macro MESSAGE
which expands to the sequence of characters "Hello, World!". The outer shell-quotes
’...’ protect the C-quotes of the string "Hello, World!". For an explanation of the
different types of quoting and escaping used in the shell see the “GNU Bash Reference
Manual”, [Further reading], page 89.
int
main (void)
{
printf ("Hello, world!\n");
return 0;
}
It is possible to see the declarations from the included header file by preprocessing the file
with gcc -E:
$ gcc -E hello.c
On a GNU system, this produces output similar to the following:
# 1 "hello.c"
# 1 "/usr/include/stdio.h" 1 3
# 1 "hello.c" 2
int
Chapter 4: Using the preprocessor 33
main (void)
{
printf ("Hello, world!\n");
return 0;
}
The preprocessed system header files usually generate a lot of output. This can be redirected
to a file, or saved more conveniently using the gcc ‘-save-temps’ option:
$ gcc -c -save-temps hello.c
After running this command, the preprocessed output will be available in the file ‘hello.i’.
The ‘-save-temps’ option also saves ‘.s’ assembly files and ‘.o’ object files in addition to
preprocessed ‘.i’ files.
Chapter 5: Compiling for debugging 34
int
main (void)
{
int *p = 0; /* null pointer */
return foo (p);
}
int
foo (int *p)
{
int y = *p;
return y;
}
1
The terminology dates back to the time of magnetic core memory.
Chapter 5: Compiling for debugging 35
(gdb) run
Starting program: a.out
Breakpoint 1, main () at null.c:6
6 int *p = 0; /* null pointer */
(gdb)
The display shows the line that will be executed next (the line number is shown on the left).
The breakpoint stops the program before the line is executed, so at this stage the pointer p
is undefined and has not yet been set to zero.
5
http://www.network-theory.co.uk/gcc/intro/
Chapter 6: Compiling with optimization 39
and memory locations, jump to the start of the function (bringing the appropriate virtual
memory pages into physical memory or the CPU cache if necessary), begin executing the
code, and then return to the original point of execution when the function call is complete.
This additional work is referred to as function-call overhead. Function inlining eliminates
this overhead by replacing calls to a function by the code of the function itself (known as
placing the code in-line).
In most cases, function-call overhead is a negligible fraction of the total run-time of a
program. It can become significant only when there are functions which contain relatively
few instructions, and these functions account for a substantial fraction of the run-time—in
this case the overhead then becomes a large proportion of the total run-time.
Inlining is always favorable if there is only one point of invocation of a function. It
is also unconditionally better if the invocation of a function requires more instructions
(memory) than moving the body of the function in-line. This is a common situation for
simple accessor functions in C++, which can benefit greatly from inlining. Moreover, inlining
may facilitate further optimizations, such as common subexpression elimination, by merging
several separate functions into a single large function.
The following function sq(x) is a typical example of a function that would benefit from
being inlined. It computes x2 , the square of its argument x:
double
sq (double x)
{
return x * x;
}
This function is small, so the overhead of calling it is comparable to the time taken to execute
the single multiplication carried out by the function itself. If this function is used inside a
loop, such as the one below, then the function-call overhead would become substantial:
for (i = 0; i < 1000000; i++)
{
sum += sq (i + 0.5);
}
Optimization with inlining replaces the inner loop of the program with the body of the
function, giving the following code:
for (i = 0; i < 1000000; i++)
{
double t = (i + 0.5); /* temporary variable */
sum += t * t;
}
Eliminating the function call and performing the multiplication in-line allows the loop to
run with maximum efficiency.
GCC selects functions for inlining using a number of heuristics, such as the function
being suitably small. As an optimization, inlining is carried out only within each object
file. The inline keyword can be used to request explicitly that a specific function should
be inlined wherever possible, including its use in other files.2 The GCC Reference Manual
2
In this case, the definition of the inline function must be made available to the other files (e.g. in a
header file).
Chapter 6: Compiling with optimization 41
“Using GCC” provides full details of the inline keyword, and its use with the static and
extern qualifiers to control the linkage of explicitly inlined functions (see [Further reading],
page 89).
6.3 Scheduling
The lowest level of optimization is scheduling, in which the compiler determines the best
ordering of individual instructions. Most CPUs allow one or more new instructions to start
executing before others have finished. Many CPUs also support pipelining, where multiple
instructions execute in parallel on the same CPU.
When scheduling is enabled, instructions must be arranged so that their results become
available to later instructions at the right time, and to allow for maximum parallel execution.
Scheduling improves the speed of an executable without increasing its size, but requires
additional memory and time in the compilation process itself (due to its complexity).
smaller and faster than with ‘-O0’. The more expensive optimizations, such as
instruction scheduling, are not used at this level.
Compiling with the option ‘-O1’ can often take less time than compiling with
‘-O0’, due to the reduced amounts of data that need to be processed after simple
optimizations.
‘-O2’ This option turns on further optimizations, in addition to those used by ‘-O1’.
These additional optimizations include instruction scheduling. Only optimiza-
tions that do not require any speed-space tradeoffs are used, so the executable
should not increase in size. The compiler will take longer to compile programs
and require more memory than with ‘-O1’. This option is generally the best
choice for deployment of a program, because it provides maximum optimization
without increasing the executable size. It is the default optimization level for
releases of GNU packages.
‘-O3’ This option turns on more expensive optimizations, such as function inlining, in
addition to all the optimizations of the lower levels ‘-O2’ and ‘-O1’. The ‘-O3’
optimization level may increase the speed of the resulting executable, but can
also increase its size. Under some circumstances where these optimizations are
not favorable, this option might actually make a program slower.
‘-funroll-loops’
This option turns on loop-unrolling, and is independent of the other optimiza-
tion options. It will increase the size of an executable. Whether or not this
option produces a beneficial result has to be examined on a case-by-case basis.
‘-Os’ This option selects optimizations which reduce the size of an executable. The
aim of this option is to produce the smallest possible executable, for systems
constrained by memory or disk space. In some cases a smaller executable will
also run faster, due to better cache usage.
It is important to remember that the benefit of optimization at the highest levels must be
weighed against the cost. The cost of optimization includes greater complexity in debugging,
and increased time and memory requirements during compilation. For most purposes it is
satisfactory to use ‘-O0’ for debugging, and ‘-O2’ for development and deployment.
6.5 Examples
The following program will be used to demonstrate the effects of different optimization
levels:
#include <stdio.h>
double
powern (double d, unsigned n)
{
double x = 1.0;
unsigned j;
return x;
}
int
main (void)
{
double sum = 0.0;
unsigned i;
sys 0m0.000s
if (x > 0)
s = 1;
else if (x < 0)
s = -1;
return s;
}
The function works correctly for most arguments, but has a bug when x is zero—in this
case the return value of the variable s will be undefined.
Compiling the program with the ‘-Wall’ option alone does not produce any warnings,
because data-flow analysis is not carried out without optimization:
$ gcc -Wall -c uninit.c
To produce a warning, the program must be compiled with ‘-Wall’ and optimization si-
multaneously. In practice, the optimization level ‘-O2’ is needed to give good warnings:
int
main ()
{
std::cout << "Hello, world!\n";
return 0;
}
The program can be compiled with the following command line:
$ g++ -Wall hello.cc -o hello
The C++ frontend of GCC uses many of the same the same options as the C compiler gcc.
It also supports some additional options for controlling C++ language features, which will
be described in this chapter. Note that C++ source code should be given one of the valid
C++ file extensions ‘.cc’, ‘.cpp’, ‘.cxx’ or ‘.C’ rather than the ‘.c’ extension used for C
programs.
The resulting executable can be run in exactly same way as the C version, simply by
typing its filename:
$ ./hello
Hello, world!
The executable produces the same output as the C version of the program, using std::cout
instead of the C printf function. All the options used in the gcc commands in previous
chapters apply to g++ without change, as do the procedures for compiling and linking files
and libraries (using g++ instead of gcc, of course). One natural difference is that the ‘-ansi’
option requests compliance with the C++ standard, instead of the C standard, when used
with g++.
Note that programs using C++ object files must always be linked with g++, in order
to supply the appropriate C++ libraries. Attempting to link a C++ object file with the C
compiler gcc will cause “undefined reference” errors for C++ standard library functions:
Chapter 7: Compiling a C++ program 48
guidelines, so you may wish to use this option as an occasional test for possible
problems in your own code rather than compiling with it all the time.
‘-Wold-style-cast’
This option highlights any uses of C-style casts in C++ programs. The C++ lan-
guage provides the keywords static_cast, dynamic_cast, reinterpret_cast
and const_cast for handling casts and these are often preferable (although
C-style casts are still allowed).
int
main ()
{
string s1 = "Hello,";
string s2 = "World!";
cout << s1 + " " + s2 << ’\n’;
return 0;
}
The program can be compiled and run using the same commands as above:
$ g++ -Wall hellostr.cc
$ ./a.out
Hello, World!
Note that in accordance with the C++ standard, the header files for the C++ library itself
do not use a file extension. The classes in the library are also defined in the std namespace,
so the directive using namespace std is needed to access them, unless the prefix std:: is
used throughout (as in the previous section).
7.4 Templates
Templates provide the ability to define C++ classes which support generic programming
techniques. Templates can be considered as a powerful kind of macro facility. When a
templated class or function is used with a specific class or type, such as float or int,
the corresponding template code is compiled with that type substituted in the appropriate
places.
The following program demonstrates the use of the template library by creating a list of
strings with the template list<string>:
#include <list>
#include <string>
#include <iostream>
int
main ()
{
list<string> list;
list.push_back("Hello");
list.push_back("World");
cout << "List size = " << list.size() << ’\n’;
return 0;
}
No special options are needed to use the template classes in the standard library; the
command-line options for compiling this program are the same as before:
$ g++ -Wall string.cc
$ ./a.out
List size = 2
Note that the executables created by g++ using the C++ standard library will be linked to the
shared library ‘libstdc++’, which is supplied as part of the default GCC installation. There
are several versions of this library—if you distribute executables using the C++ standard
library you need to ensure that the recipient has a compatible version of ‘libstdc++’, or
link your program statically using the command-line option ‘-static’.
#endif /* BUFFER_H */
The file contains both the declaration of the class and the definitions of the member func-
tions. This class is only given for demonstration purposes and should not be considered an
example of good programming. Note the use of include guards, which test for the presence
of the macro BUFFER_H, ensuring that the definitions in the header file are only parsed once
if the file is included multiple times in the same context.
The program below uses the templated Buffer class to create a buffer of size 10, storing
the floating point values 0.25 and 1.25 in the buffer:
#include <iostream>
#include "buffer.h"
int
main ()
{
Chapter 7: Compiling a C++ program 52
Buffer<float> f(10);
f.insert (0.25);
f.insert (1.0 + f.get(0));
cout << "stored value = " << f.get(0) << ’\n’;
return 0;
}
The definitions for the template class and its functions are included in the source file for
the program with ‘#include "buffer.h"’ before they are used. The program can then be
compiled using the following command line:
$ g++ -Wall tprog.cc
$ ./a.out
stored value = 1.25
At the points where the template functions are used in the source file, g++ compiles the
appropriate definition from the header file and places the compiled function in the corre-
sponding object file.
If a template function is used several times in a program it will be stored in more than
one object file. The GNU Linker ensures that only one copy is placed in the final executable.
Other linkers may report “multiply defined symbol” errors when they encounter more than
one copy of a template function—a method of working with these linkers is described below.
The object code for all the template functions is contained in the file ‘templates.o’.
There is no object code for template functions in ‘tprog.o’ when it is compiled with the
‘-fno-implicit-templates’ option.
If the program is modified to use additional types, then further explicit instantiations can
be added to the file ‘templates.cc’. For example, the following code adds instantiations
for Buffer objects containing double and int values:
#include "buffer.h"
template class Buffer<float>;
template class Buffer<double>;
template class Buffer<int>;
The disadvantage of explicit instantiation is that it is necessary to know which template
types are needed by the program. For a complicated program this may be difficult to
determine in advance. Any missing template instantiations can be determined at link time,
however, and added to the list of explicit instantiations, by noting which functions are
undefined.
Explicit instantiation can also be used to make libraries of precompiled template func-
tions, by creating an object file containing all the required instantiations of a template
function (as in the file ‘templates.cc’ above). For example, the object file created from
the template instantiations above contains the machine code needed for Buffer classes with
‘float’, ‘double’ and ‘int’ types, and could be distributed in a library.
8 Platform-specific options
GCC provides a range of platform-specific options for different types of CPUs. These options
control features such as hardware floating-point modes, and the use of special instructions
for different CPUs. They can be selected with the ‘-m’ option on the command line, and
work with all the GCC language frontends, such as gcc and g++.
The following sections describe some of the options available for common platforms. A
complete list of all platform-specific options can be found in the GCC Reference Manual,
“Using GCC” (see [Further reading], page 89). Support for new processors is added to
GCC as they become available, therefore some of the options described in this chapter may
not be found in older versions of GCC.
processors supporting the appropriate extensions—on other systems they will crash with
an Illegal instruction error (or similar).3
The option ‘-mfpmath=sse’ instructs GCC to use the SSE extensions for floating-point
arithmetic where possible. For this option to take effect, the SSE or SSE2 extensions must
first be enabled with ‘-msse’ or ‘-msse2’.
Note that the plain SSE extensions only support single precision operations—double
precision arithmetic is part of SSE2. Since most C and C++ programs declare floating-point
variables as double rather than float, the combined options -msse2 -mfpmath=sse are
usually needed. On 64-bit processors these options are enabled by default.
#include <stdio.h>
int
main (void)
{
double x = 1.0, y = 0.0;
printf ("x/y = %g\n", x / y);
return 0;
}
In IEEE arithmetic the result of 1/0 is inf (Infinity). If the program is compiled for the
Alpha processor with the default settings it generates an exception, which terminates the
program:
$ gcc -Wall alpha.c
$ ./a.out
Floating point exception (on an Alpha processor)
Using the ‘-mieee’ option ensures full IEEE compliance – the division 1/0 correctly produces
the result inf and the program continues executing successfully:
$ gcc -Wall -mieee alpha.c
$ ./a.out
x/y = inf
Note that programs which generate floating-point exceptions run more slowly when compiled
with ‘-mieee’, because the exceptions are handled in software rather than hardware.
the way intermediate values are rounded, the result of a fused instruction may not be
exactly the same as performing the two operations separately. In cases where strict IEEE
arithmetic is required, the use of the combined instructions can be disabled with the option
‘-mno-fused-madd’.
On AIX systems, the option ‘-mminimal-toc’ decreases the number of entries GCC puts
in the global table of contents (TOC) in executables, to avoid “TOC overflow” errors at
link time. The option ‘-mxl-call’ makes the linking of object files from GCC compatible
with those from IBM’s XL compilers. For applications using POSIX threads, AIX always
requires the option ‘-pthread’ when compiling, even when the program will only run in
single-threaded mode.
5
The options ‘-maix64’ and ‘-maix32’ are used on AIX.
6
Motorola 68k processors also use extended precision registers, like the x86.
Chapter 8: Platform-specific options 58
a greater relative precision and exponent range when computed in extended precision.7 In
particular, comparisons involving extended precision values may fail where the equivalent
double precision values would compare equal.
To avoid these incompatibilities, the x87 FPU also offers a hardware double-precision
rounding mode. In this mode the results of each extended-precision floating-point operation
are rounded to double precision in the floating-point registers by the FPU. It is important
to note that the rounding only affects the precision, not the exponent range, so the result
is a hybrid double-precision format with an extended range of exponents.
On BSD systems such as FreeBSD, NetBSD and OpenBSD, the hardware double-
precision rounding mode is the default, giving the greatest compatibility with native
double precision platforms. On x86 GNU/Linux systems the default mode is extended
precision (with the aim of providing increased accuracy). To enable the double-precision
rounding mode it is necessary to override the default setting on per-process basis using
the fldcw “floating-point load control-word” machine instruction.8 A simple function
which can be called to execute this instruction is shown below. It uses the GCC extension
keyword asm to insert the specified instruction in the assembly language output:
void
set_fpu (unsigned int mode)
{
asm ("fldcw %0" : : "m" (*&mode));
}
The appropriate mode setting for double-precision rounding is 0x27F. The mode value also
controls the floating-point exception handling behavior and rounding-direction (see the Intel
and AMD processor reference manuals for details).
On x86 GNU/Linux, the function above can be called at the start of any program to
disable excess precision. This will then reproduce the results of native double-precision
processors, in the absence of underflows and overflows.
The following program demonstrates the different rounding modes:
#include <stdio.h>
void
set_fpu (unsigned int mode)
{
asm ("fldcw %0" : : "m" (*&mode));
}
int
main (void)
{
double a = 3.0, b = 7.0, c;
7
For quantities held in the x87 extended-precision registers the relative precision is 5.42 × 10−20 and the
exponent range is 10±4932 . Standard double precision values have a relative precision of 2.22 × 10−16
and an exponent range of 10±308 .
8
The operating system saves and restores the control word when switching between processes, so that
each process maintains its own setting.
Chapter 8: Platform-specific options 59
#ifdef DOUBLE
set_fpu (0x27F); /* use double-precision rounding */
#endif
c = a / b;
if (c == a / b) {
printf ("comparison succeeds\n");
} else {
printf ("unexpected result\n");
}
return 0;
}
On x86 GNU/Linux systems the comparison c == a / b can produce an unexpected result
if c is taken from memory (double precision) while a / b is computed in extended precision,
because the fraction 3/7 has different representations in double and extended precision.
$ gcc -Wall fptest.c
$ ./a.out
unexpected result
Setting the hardware rounding mode to double precision prevents this from happening:
$ gcc -Wall -DDOUBLE fptest.c
$ ./a.out
comparison succeeds
Note that the floating-point control word affects the whole environment of the process,
including any C Library functions that are called. One consequence of this is that long
double arithmetic is effectively reduced to double precision, since it relies on extended
precision operations.
The floating point control word only affects the behavior of the x87 FPU. Floating point
operations computed with SSE and SSE2 instructions are always carried out in native
double precision. Thus, the combined options
$ gcc -Wall -msse2 -mfpmath=sse ...
are often sufficient to remove the effects of extended-precision. However, some operations
(such as transcendental functions) are not available in the SSE/SSE2 extensions and will
still be computed on the x87 FPU.
#include <stdio.h>
int
main (void)
{
char c = 255;
if (c > 128) {
printf ("char is unsigned (c = %d)\n", c);
} else {
printf ("char is signed (c = %d)\n", c);
}
return 0;
}
With an unsigned char, the variable c takes the value 255, but with a signed char it
becomes −1.
The correct way to manipulate char variables in C is through the portable functions
declared in ‘ctype.h’, such as isalpha, isdigit and isblank, rather than by their numer-
ical values. The behavior of non-portable conditional expressions such as c > ’a’ depends
on the signedness of the char type. If the signed or unsigned version of char is explicitly
required at certain points in a program, it can be specified using the declarations signed
char or unsigned char.
For existing programs which assume that char is signed or unsigned, GCC provides the
options ‘-fsigned-char’ and ‘-funsigned-char’ to set the default type of char. Using
these options, the example code above compiles cleanly when char is unsigned:
$ gcc -Wall -funsigned-char signed.c
$ ./a.out
char is unsigned (c = 255)
However, when char is signed the value 255 wraps around to −1, giving a warning:
$ gcc -Wall -fsigned-char signed.c
signed.c: In function ‘main’:
signed.c:7: warning: comparison is always false due to
limited range of data type
$ ./a.out
char is signed (c = -1)
The warning message “comparison is always true/false due to limited range of data type” is
one symptom of code which assumes a definition of char which is different from the actual
type.
The most common problem with code written assuming signed char types occurs with
the functions getc, fgetc and getchar (which read a character from a file). They have
a return type of int, not char, and this allows them to use the special value −1 (defined
as EOF) to indicate an end-of-file error. Unfortunately, many programs have been written
which incorrectly store this return value straight into a char variable. Here is a typical
example:
#include <stdio.h>
Chapter 8: Platform-specific options 61
int
main (void)
{
char c;
while ((c = getchar()) != EOF) /* not portable */
{
printf ("read c = ’%c’\n", c);
}
return 0;
}
This only works on platforms which default to a signed char type.10 On platforms which
use an unsigned char the same code will fail, because the value −1 becomes 255 when
stored in an unsigned char. This usually causes an infinite loop because the end of the
file cannot be recognized.11 To be portable, the program should test the return value as an
integer before coercing it to a char, as follows:
#include <stdio.h>
int
main (void)
{
int i;
while ((i = getchar()) != EOF)
{
unsigned char c = i;
printf ("read c = ’%c’\n", c);
}
return 0;
}
The same considerations described in this section apply to the definitions of bitfields in
structs, which can be signed or unsigned by default. In GCC, the default type of bitfields
can be controlled using the options ‘-fsigned-bitfields’ and ‘-funsigned-bitfields’.
10
There is also a subtle error even on platforms with signed char—the ascii character 255 is spuriously
interpreted as an end of file condition.
11
If displayed, character code 255 often appears as ÿ.
Chapter 9: Troubleshooting 62
9 Troubleshooting
GCC provides several help and diagnostic options to assist in troubleshooting problems
with the compilation process. All the options described in this chapter work with both gcc
and g++.
return 0;
}
In order to attach to the program and debug it, the code should be compiled with the
debugging option ‘-g’:
$ gcc -Wall -g loop.c
$ ./a.out
(program hangs)
Once the executable is running we need to find its process id (pid). This can be done from
another session with the command ps x:
$ ps x
PID TTY STAT TIME COMMAND
... ..... . ....
891 pts/1 R 0:11 ./a.out
In this case the process id is 891, and we can now attach to it with gdb. The debugger
should be started in the directory containing the executable and its source code:1
$ gdb a.out
(gdb) attach 891
Attaching to program: a.out, process 891
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
0x080483d4 in main () at loop.c:5
5 while (1) { i++; };
(gdb)
The output shows the line that was about to execute at the point when the debugger
attached to the process. The attached program is paused but still “live”—it can be examined
interactively and continued or terminated (with the kill command) if necessary:
(gdb) print i
$1 = 1213315528
(gdb) kill
Kill the program being debugged? (y or n) y
(gdb)
If you want to stop a process immediately and create a core dump, the shell command kill
-3 pid (where pid is the process id) will send it a sigquit signal. The sigquit signal does
trigger a core dump, unlike sigint. Note that if core dumps were disabled when the process
was started, no core file will be produced (see Section 5.1 [Examining core files], page 34).
limit is measured in kilobytes and applies to new processes started in the current shell. For
example,
$ ulimit -v 4096
will limit subsequent processes to 4 megabytes of virtual memory (4096k). By default the
limit cannot be increased in the same session once it has been applied, so it is best to start a
separate shell for reduced ulimit operations. Alternatively, you can set a soft limit (which
can be undone) with the options ‘-S -v’.
In addition to preventing run-away processes, limiting the amount of memory a program
is allowed to allocate also provides a way to test how robustly out of memory conditions
are handled. An artificially low limit can be used to simulate running out of memory—a
well-written program should not crash in this case.
The ulimit command supports other options including ‘-p’, which restricts the number
of child processes that can be created, and ‘-t’, which places a limit on the number of cpu
seconds that a process can run for. The complete list of settings can be shown with the
command ulimit -a. To display more information about the ulimit command, type help
ulimit at the Bash prompt.
Chapter 10: Compiler-related tools 66
10 Compiler-related tools
This chapter describes a number of tools which are useful in combination with GCC. These
include the GNU archiver ar, for creating libraries, and the GNU profiling and coverage
testing programs, gprof and gcov.
void
hello (const char * name)
{
printf ("Hello, %s!\n", name);
}
The second object file will be generated from the source file ‘bye_fn.c’, which contains the
new function bye:
#include <stdio.h>
#include "hello.h"
void
bye (void)
{
printf ("Goodbye!\n");
}
Both functions use the header file ‘hello.h’, now with a prototype for the function bye():
void hello (const char * name);
void bye (void);
The source code can be compiled to the object files ‘hello_fn.o’ and ‘bye_fn.o’ using the
commands:
$ gcc -Wall -c hello_fn.c
$ gcc -Wall -c bye_fn.c
These object files can be combined into a static library using the following command line:
$ ar cr libhello.a hello_fn.o bye_fn.o
Chapter 10: Compiler-related tools 67
The option ‘cr’ stands for “create and replace”.1 If the library does not exist, it is first
created. If the library already exists, any original files in it with the same names are replaced
by the new files specified on the command line. The first argument ‘libhello.a’ is the
name of the library. The remaining arguments are the names of the object files to be copied
into the library.
The archiver ar also provides a “table of contents” option ‘t’ to list the object files in
an existing library:
$ ar t libhello.a
hello_fn.o
bye_fn.o
Note that when a library is distributed, the header files for the public functions and variables
it provides should also be made available, so that the end-user can include them and obtain
the correct prototypes.
We can now write a program using the functions in the newly created library:
#include "hello.h"
int
main (void)
{
hello ("everyone");
bye ();
return 0;
}
This file can be compiled with the following command line, as described in Section 2.7
[Linking with external libraries], page 11, assuming the library ‘libhello.a’ is stored in
the current directory:
$ gcc -Wall main.c libhello.a -o hello
The main program is linked against the object files found in the library file ‘libhello.a’
to produce the final executable.
The short-cut library linking option ‘-l’ can also be used to link the program, without
needing to specify the full filename of the library explicitly:
$ gcc -Wall -L. main.c -lhello -o hello
The option ‘-L.’ is needed to add the current directory to the library search path. The
resulting executable can be run as usual:
$ ./hello
Hello, everyone!
Goodbye!
It displays the output from both the hello and bye functions defined in the library.
1
Note that ar does not require a prefix ‘-’ for its options.
Chapter 10: Compiler-related tools 68
unsigned int
step (unsigned int x)
{
if (x % 2 == 0)
{
return (x / 2);
}
else
{
return (3 * x + 1);
}
}
unsigned int
nseq (unsigned int x0)
{
unsigned int i = 1, x;
if (x0 == 1 || x0 == 0)
return i;
x = step (x0);
while (x != 1 && x != 0)
{
x = step (x);
i++;
}
return i;
}
2
American Mathematical Monthly, Volume 92 (1985), 3–23
Chapter 10: Compiler-related tools 69
int
main (void)
{
unsigned int i, m = 0, im = 0;
if (k > m)
{
m = k;
im = i;
printf ("sequence length = %u for %u\n", m, im);
}
}
return 0;
}
To use profiling, the program must be compiled and linked with the ‘-pg’ profiling option:
$ gcc -Wall -c -pg collatz.c
$ gcc -Wall -pg collatz.o
This creates an instrumented executable which contains additional instructions that record
the time spent in each function.
If the program consists of more than one source file then the ‘-pg’ option should be used
when compiling each source file, and used again when linking the object files to create the
final executable (as shown above). Forgetting to link with the option ‘-pg’ is a common
error, which prevents profiling from recording any useful information.
The executable must be run to create the profiling data:
$ ./a.out
(normal program output is displayed)
While running the instrumented executable, profiling data is silently written to a file
‘gmon.out’ in the current directory. It can be analyzed with gprof by giving the name
of the executable as an argument:
$ gprof a.out
Flat profile:
Each sample counts as 0.01 seconds.
% cumul. self self total
time seconds seconds calls us/call us/call name
68.59 2.14 2.14 62135400 0.03 0.03 step
31.09 3.11 0.97 499999 1.94 6.22 nseq
0.32 3.12 0.01 main
The first column of the data shows that the program spends most of its time (almost 70%)
in the function step, and 30% in nseq. Consequently efforts to decrease the run-time of
the program should concentrate on the former. In comparison, the time spent within the
main function itself is completely negligible (less than 1%).
The other columns in the output provide information on the total number of function
calls made, and the time spent in each function. Additional output breaking down the
Chapter 10: Compiler-related tools 70
run-time further is also produced by gprof but not shown here. Full details can be found
in the manual “GNU gprof—The GNU Profiler”, by Jay Fenlason and Richard Stallman.
int
main (void)
{
int i;
return 0;
}
To enable coverage testing the program must be compiled with the following options:
$ gcc -Wall -fprofile-arcs -ftest-coverage cov.c
This creates an instrumented executable which contains additional instructions that record
the number of times each line of the program is executed. The option ‘-ftest-coverage’
adds instructions for counting the number of times individual lines are executed, while
‘-fprofile-arcs’ incorporates instrumentation code for each branch of the program.
Branch instrumentation records how frequently different paths are taken through ‘if’
statements and other conditionals. The executable must then be run to create the coverage
data:
$ ./a.out
3 is divisible by 3
6 is divisible by 3
9 is divisible by 3
The data from the run is written to several files with the extensions ‘.bb’ ‘.bbg’ and ‘.da’
respectively in the current directory. This data can be analyzed using the gcov command
and the name of a source file:
Chapter 10: Compiler-related tools 71
$ gcov cov.c
88.89% of 9 source lines executed in file cov.c
Creating cov.c.gcov
The gcov command produces an annotated version of the original source file, with the file
extension ‘.gcov’, containing counts of the number of times each line was executed:
#include <stdio.h>
int
main (void)
{
1 int i;
1 return 0;
1 }
The line counts can be seen in the first column of the output. Lines which were not executed
are marked with hashes ‘######’. The command ‘grep ’######’ *.gcov’ can be used to
find parts of a program which have not been used.
Chapter 11: How the compiler works 72
int
main (void)
{
printf ("Hello, world!\n");
return 0;
}
Note that it is not necessary to use any of the individual commands described in this section
to compile a program. All the commands are executed automatically and transparently by
GCC internally, and can be seen using the ‘-v’ option described earlier (see Section 9.3
[Verbose compilation], page 62). The purpose of this chapter is to provide an understanding
of how the compiler works.
Although the Hello World program is very simple it uses external header files and li-
braries, and so exercises all the major steps of the compilation process.
The first line shows that this program depends on the math library libm (shared library
version 6), in addition to the C library and dynamic loader library.
The ldd command can also be used to examine shared libraries themselves, in order to
follow a chain of shared library dependencies.
Chapter 13: Common error messages 78
int
main (void)
{
printf ("Hello World!\n");
return 0;
}
The program above tries to include the non-existent file ‘stdoi.h’ giving the
error ‘stdoi.h: No such file or directory’. The correct filename should be
‘stdio.h’.
macro or ’#include’ recursion too deep
#include nested too deeply
This error occurs if the preprocessor encounters too many nested ‘#include’
directives. It is usually caused by two or more files trying to include each other,
leading to an infinite recursion.
Example:
/* foo.h */
#include "bar.h"
...
/* bar.h */
#include "foo.h"
...
The solution to this problem is to ensure that files do not mutually include
each other, or to use “include guards” (see Section 7.4.2 [Providing your own
templates], page 50 for an example).
invalid preprocessing directive #...
This error indicates that the preprocessor encountered an unrecognized # com-
mand.
Example:
Chapter 13: Common error messages 79
#if FOO
int x = 1;
#elsif BAR /* should be #elif */
int x = 2;
#else
int x = 3;
#endif
The preprocessor syntax requires #elif for the “else if” condition in #if blocks,
rather than #elseif. In the example above an invalid directive error occurs
at the incorrect usage #elseif, but only when FOO is defined (otherwise the
preprocessor ignores everything up to the #else statement).
warning: This file includes at least one deprecated or antiquated header.
This warning is generated for C++ programs which include old-style library
header files, such as ‘iostream.h’, instead of the modern C++ library headers
without the ‘.h’ extension. The old headers import their functions into the
top-level global namespace, instead of using the std:: namespace. Note that
old-style header files are still supported, so this message is only a warning and
existing programs will continue to compile. The message is actually generated
by a #warning directive in the old header files, and not by the preprocessor
itself.
Example:
#include <iostream.h> /* old style */
int
main (void)
{
cout << "Hello World!\n";
return 0;
}
This program uses an old-style header file ‘iostream.h’. It could be updated
to use #include <iostream> and std::cout instead.
j = 0; /* undeclared */
return j;
}
The variable j is not declared and will trigger the error ‘j’ undeclared.
parse error before ‘...’
syntax error
These error messages occur when the compiler encounters unexpected input, i.e.
sequences of characters which do not follow the syntax of the language. The
error messages can be triggered by a missing close bracket, brace or semicolon
preceding the line of the error, or an invalid keyword.
Example:
#include <stdio.h>
int
main (void)
{
printf ("Hello ") /* missing semicolon */
printf ("World!\n");
return 0;
}
There is a missing semicolon after the first call to printf, giving a parse error.
parse error at end of input
This error occurs if the compiler encounters the end of a file unexpectedly, such
as when it has parsed an unbalanced number of opening and closing braces. It
is often caused by a missing closing brace somewhere.
Example:
#include <stdio.h>
int
main (void)
{
if (1) {
printf ("Hello World!\n");
return 0; /* no closing brace */
}
An additional closing brace is needed in this program to prevent the error parse
error at end of input.
warning: implicit declaration of function ‘...’
This warning is generated when a function is used without a prototype being
declared. It can be caused by failing to include a header file, or otherwise
forgetting to provide a function prototype.
Example:
int
main (void)
Chapter 13: Common error messages 81
{
printf ("Hello World!\n"); /* no header */
return 0;
}
The system header file ‘stdio.h’ is not included, so the prototype for printf
is not declared. The program needs an initial line #include <stdio.h>.
unterminated string or character constant
This error is caused by an opening string or character quote which does not
have a corresponding closing quote. Quotes must occur in matching pairs, either
single quotes ’a’ for characters or double quotes "aaa" for strings.
Example:
#include <stdio.h>
int
main (void)
{
printf ("Hello World!\n); /* no closing quote */
return 0;
}
The opening quote for the string in this program does not have a corresponding
closing quote, so the compiler will read the rest of the file as part of the string.
character constant too long
In C and C++ character codes are written using single quotes, e.g. ’a’ gives the
ASCII code for the letter a (67), and ’\n’ gives the ASCII code for newline (10).
This error occurs if single quotes are used to enclose more than one character.
Example:
#include <stdio.h>
int
main (void)
{
printf (’Hello World!\n’); /* wrong quotes */
return 0;
}
The program above confuses single-quotes and double-quotes. A sequence of
characters should be written with double quotes, e.g. "Hello World!". This
same problem occurs in the following C++ program,
#include <iostream>
int
main (void)
{
std::cout << ’Hello World!\n’; // wrong quotes
return 0;
Chapter 13: Common error messages 82
}
This error can also occur if the forward slash and backslash are confused in an
escape sequence, e.g. using ’/n’ instead of ’\n’. The sequence /n consists of
two separate characters, ‘/’ and ‘n’.
Note that according to the C standard there is no limit on the length of a
character constant, but the value of a character constant that contains more
than one character is implementation-defined. Recent versions of GCC provide
support multi-byte character constants, and instead of an error the warnings
multiple-character character constant or warning: character constant
too long for its type are generated in this case.
warning: initialization makes integer from pointer without a cast
This error indicates a misuse of a pointer in an integer context. Technically,
it is possible to convert between integer and pointer types, but this is rarely
needed outside system-level applications. More often, this warning is the result
of using a pointer without dereferencing it (e.g. writing int i = p instead of
int i = *p).
This warning can also occur with char and char * types, since char is an
integer type.
Example:
int
main (void)
{
char c = "\n"; /* incorrect */
return 0;
}
The variable c has type char, while the string "\n" evaluates to a const char
* pointer (to a 2-byte region of memory containing the ASCII value for newline
followed by a zero byte ’\0’, since strings are null-terminated). The ASCII
code for newline can be found using char c = ’\n’;
Similar errors can occur with misuse of the macro NULL,
#include <stdlib.h>
int
main (void)
{
int i = NULL; /* incorrect */
return 0;
}
In C, the macro NULL is defined as ((void *)0) in ‘stdlib.h’ and should only
be used in a pointer context.
dereferencing pointer to incomplete type
This error occurs when a program attempts to access the elements of struct
through a pointer without the layout of the struct being declared first. In C
and C++ it is possible to declare pointers to structs before declaring their struct
Chapter 13: Common error messages 83
int
main (void)
{
data->size = 0; /* incomplete type */
return 0;
}
This program has a forward declaration of the btree struct data. However,
the definition of the struct is needed before the pointer can be dereferenced to
access individual members.
warning: unknown escape sequence ‘...’
This error is caused by an incorrect use of the escape character in a string.
Valid escape sequences are:
\n newline \t tab
\b backspace \r carriage return
\f form feed \v vertical tab
\a alert (bell)
The combinations \\, \’, \" and \? can be used for individual characters. Es-
cape sequences can also use octal codes \0–\377 and hexadecimal codes \0x00–
\0xFF.
Example:
#include <stdio.h>
int
main (void)
{
printf ("HELLO WORLD!\N");
return 0;
}
The escape sequence \N in the program above is invalid—the correct escape
sequence for a newline is \n.
warning: suggest parentheses around assignment used as truth value
This warning highlights a potentially serious error, using the assignment op-
erator ‘=’ instead of the comparison operator ‘==’ in the test of a conditional
statement or other logical expression. While the assignment operator can be
used as part of a logical value, this is rarely the intended behavior.
Example:
#include <stdio.h>
int
Chapter 13: Common error messages 84
main (void)
{
int i = 0;
if (i = 1) { /* = should be == */
printf ("unexpected result\n");
}
return 0;
}
The test above should be written as if (i == 1), otherwise the variable i will
be set to 1 by the evaluation of the if statement itself. The operator ‘=’ both
assigns and returns the value of its right-hand side, causing the variable i to be
modified and the unexpected branch taken. Similar unexpected results occur
with if (i = 0) instead of if (i == 0), except that in this case the body of the
if statement would never be executed.
This warning is suppressed if the assignment is enclosed in additional paren-
theses to indicate that it is being used legitimately.
warning: control reaches end of non-void function
A function which has been declared with a return type, such as int or double,
should always have a return statement returning a value of the corresponding
type at all possible end points—otherwise its return value is not well-defined.
Functions declared void do not need return statements.
Example:
#include <stdio.h>
int
display (const char * str)
{
printf ("%s\n", str);
}
The program above reaches the end of the display function, which has a return
type of int, without a return statement. An additional line such as return
0; is needed.
When using gcc the main function of a C program must return a value of type
int (the exit status of the program). In C++ the return statement can be
omitted from the main function—the return value of the C++ main function
defaults to 0 if unspecified.
warning: unused variable ‘...’
warning: unused parameter ‘...’
These warnings indicate that a variable has been declared as a local variable or
in the parameters of a function, but has not been used anywhere. An unused
variable can be the result of a programming error, such as accidentally using
the name of a different variable in place of the intended one.
Example:
int
Chapter 13: Common error messages 85
int
main (void)
Chapter 13: Common error messages 86
{
fprintf (stream, "Hello World!\n");
return 0;
}
This program attempts to initialize two variables from other variables. In par-
ticular, the stream stdout is not required to be a constant by the C standard
(although on some systems it is a constant). Note that non-constant initializers
are allowed in C++.
int
main (void)
{
printf ("Hello World!\n");
return 0;
}
If the program above is saved in a file ‘hello’ without any extension then
compiling it will give the error:
$ gcc -Wall hello
hello: file not recognized: File format not
recognized
collect2: ld returned 1 exit status
The solution is to rename the file to the correct extension, in this case ‘hello.c’.
undefined reference to ‘foo’
collect2: ld returned 1 exit status
This error occurs when a program uses a function or variable which is not
defined in any of the object files or libraries supplied to the linker. It can be
caused by a missing library or the use of an incorrect name. In the error message
above, the program ‘collect2’ is part of the linker.
Example:
int foo(void);
int
main (void)
{
foo();
return 0;
Chapter 13: Common error messages 87
}
If this program is compiled without linking to a library or object file containing
the function foo() there will be an undefined reference error.
/usr/lib/crt1.o(.text+0x18): undefined reference to ‘main’
This error is a special case of the error above, when the missing function is main.
In C and C++, every program must have a main function (where execution
starts). When compiling an individual source file without a main function,
use the option ‘-c’ (see Section 2.4.1 [Creating object files from source files],
page 8).
14 Getting help
If you encounter a problem not covered by this introduction, there are several reference
manuals which describe GCC and language-related topics in more detail (see [Further read-
ing], page 89). These manuals contain answers to common questions, and careful study of
them will usually yield a solution.
Alternatively, there are many companies and consultants who offer commercial support
for programming matters related to GCC on an hourly or ongoing basis. For businesses
this can be a cost-effective way to obtain high-quality support.
A directory of free software support companies and their current rates can be found on
the GNU Project website.1 With free software, commercial support is available in a free
market—service companies compete in quality and price, and users are not tied to any
particular one. In contrast, support for proprietary software is usually only available from
the original vendor.
A higher-level of commercial support for GCC is available from companies involved in
the development of the GNU compiler toolchain itself. A listing of these companies can be
found in the “Development Companies” section of the publisher’s webpage for this book.2
These companies can provide services such as extending GCC to generate code for new
CPUs or fixing bugs in the compiler.
1
http://www.gnu.org/prep/service.html
2
http://www.network-theory.co.uk/gcc/intro/
Chapter 14: Further reading 89
Further reading
The definitive guide to GCC is the official reference manual, “Using GCC”, published by
GNU Press:
Using GCC (for GCC version 3.3.1) by Richard M. Stallman and the GCC
Developer Community (Published by GNU Press, ISBN 1-882114-39-6)
This manual is essential for anyone working with GCC because it describes every option in
detail. Note that the manual is updated when new releases of GCC become available, so
the ISBN number may change in the future.
If you are new to programming with GCC you will also want to learn how to use the
GNU Debugger GDB, and how to compile large programs easily with GNU Make. These
tools are described in the following manuals:
Debugging with GDB: The GNU Source-Level Debugger by Richard M. Stall-
man, Roland Pesch, Stan Shebs, et al. (Published by GNU Press, ISBN 1-
882114-88-4)
GNU Make: A Program for Directing Recompilation by Richard M. Stallman
and Roland McGrath (Published by GNU Press, ISBN 1-882114-82-5)
For effective C programming it is also essential to have a good knowledge of the C standard
library. The following manual documents all the functions in the GNU C Library:
The GNU C Library Reference Manual by Sandra Loosemore with Richard M.
Stallman, et al (2 vols) (Published by GNU Press, ISBN 1-882114-22-1 and
1-882114-24-8)
Be sure to check the website http://www.gnupress.org/ for the latest printed editions of
manuals published by GNU Press. The manuals can be purchased online using a credit card
at the FSF website1 in addition to being available for order through most bookstores using
the ISBNs. Manuals published by GNU Press raise funds for the Free Software Foundation
and the GNU Project.
Information about shell commands, environment variables and shell-quoting rules can
be found in the following book:
The GNU Bash Reference Manual by Chet Ramey and Brian Fox (Published
by Network Theory Ltd, ISBN 0-9541617-7-7)
Other GNU Manuals mentioned in this book (such as GNU gprof—The GNU Profiler and
The GNU Binutils Manual) were not available in print at the time this book went to press.
Links to online copies can be found at the publisher’s webpage for this book.2
The official GNU Project webpage for GCC can be found on the GNU website at
http://www.gnu.org/software/gcc/. This includes a list of frequently asked questions,
as well as the GCC bug tracking database and a lot of other useful information about GCC.
There are many books about the C and C++ languages themselves. Two of the standard
references are:
The C Programming Language (ANSI edition) Brian W. Kernighan, Dennis
Ritchie (ISBN 0-13110362-8)
1
http://order.fsf.org/
2
http://www.network-theory.co.uk/gcc/intro/
Chapter 14: Further reading 90
Acknowledgements
Many people have contributed to this book, and it is important to record their names here:
Thanks to Gerald Pfeifer, for his careful reviewing and numerous suggestions for im-
proving the book.
Thanks to Andreas Jaeger, for information on AMD64 and multi-architecture support,
and many helpful comments.
Thanks to David Edelsohn, for information on the POWER/PowerPC series of proces-
sors.
Thanks to Jamie Lokier, for research.
Thanks to Martin Leisner, Mario Pernici, Stephen Compall and Nigel Lowry, for helpful
corrections.
Thanks to Gerard Jungman, for useful comments.
Thanks to Steven Rubin, for generating the chip layout for the cover with Electric.
And most importantly, thanks to Richard Stallman, founder of the GNU Project, for
writing GCC and making it free software.
Chapter 14: Other books from the publisher 92
have any title page as such, “Title Page” means the text near the most prominent appearance of the work’s
title, preceding the beginning of the body of the text.
A section “Entitled XYZ” means a named subunit of the Document whose title either is precisely XYZ or
contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for
a specific section name mentioned below, such as “Acknowledgements”, “Dedications”, “Endorsements”, or
“History”.) To “Preserve the Title” of such a section when you modify the Document means that it remains a
section “Entitled XYZ” according to this definition.
The Document may include Warranty Disclaimers next to the notice which states that this License applies to
the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only
as regards disclaiming warranties: any other implication that these Warranty Disclaimers may have is void and
has no effect on the meaning of this License.
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or noncommercially, provided
that this License, the copyright notices, and the license notice saying this License applies to the Document
are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You
may not use technical measures to obstruct or control the reading or further copying of the copies you make
or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough
number of copies you must also follow the conditions in section 3.
You may also lend copies, under the same conditions stated above, and you may publicly display copies.
3. COPYING IN QUANTITY
If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering
more than 100, and the Document’s license notice requires Cover Texts, you must enclose the copies in covers
that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover
Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies.
The front cover must present the full title with all words of the title equally prominent and visible. You may add
other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects.
If the required texts for either cover are too voluminous to fit legibly, you should put the first ones listed (as
many as fit reasonably) on the actual cover, and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include
a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a
computer-network location from which the general network-using public has access to download using public-
standard network protocols a complete Transparent copy of the Document, free of added material. If you use
the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in
quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least
one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that
edition to the public.
It is requested, but not required, that you contact the authors of the Document well before redistributing any
large number of copies, to give them a chance to provide you with an updated version of the Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the conditions of sections 2 and 3
above, provided that you release the Modified Version under precisely this License, with the Modified Version
filling the role of the Document, thus licensing distribution and modification of the Modified Version to whoever
possesses a copy of it. In addition, you must do these things in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those
of previous versions (which should, if there were any, be listed in the History section of the Document).
You may use the same title as a previous version if the original publisher of that version gives permission.
B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the
modifications in the Modified Version, together with at least five of the principal authors of the Document
(all of its principal authors, if it has fewer than five), unless they release you from this requirement.
C. State on the Title page the name of the publisher of the Modified Version, as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the other copyright notices.
F. Include, immediately after the copyright notices, a license notice giving the public permission to use the
Modified Version under the terms of this License, in the form shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the
Document’s license notice.
H. Include an unaltered copy of this License.
Chapter 14: GNU Free Documentation License 96
I. Preserve the section Entitled “History”, Preserve its Title, and add to it an item stating at least the
title, year, new authors, and publisher of the Modified Version as given on the Title Page. If there is no
section Entitled “History” in the Document, create one stating the title, year, authors, and publisher of
the Document as given on its Title Page, then add an item describing the Modified Version as stated in
the previous sentence.
J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the
Document, and likewise the network locations given in the Document for previous versions it was based
on. These may be placed in the “History” section. You may omit a network location for a work that was
published at least four years before the Document itself, or if the original publisher of the version it refers
to gives permission.
K. For any section Entitled “Acknowledgements” or “Dedications”, Preserve the Title of the section, and
preserve in the section all the substance and tone of each of the contributor acknowledgements and/or
dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section
numbers or the equivalent are not considered part of the section titles.
M. Delete any section Entitled “Endorsements”. Such a section may not be included in the Modified Version.
N. Do not retitle any existing section to be Entitled “Endorsements” or to conflict in title with any Invariant
Section.
O. Preserve any Warranty Disclaimers.
If the Modified Version includes new front-matter sections or appendices that qualify as Secondary Sections and
contain no material copied from the Document, you may at your option designate some or all of these sections
as invariant. To do this, add their titles to the list of Invariant Sections in the Modified Version’s license notice.
These titles must be distinct from any other section titles.
You may add a section Entitled “Endorsements”, provided it contains nothing but endorsements of your Modified
Version by various parties—for example, statements of peer review or that the text has been approved by an
organization as the authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of up to 25 words as a Back-
Cover Text, to the end of the list of Cover Texts in the Modified Version. Only one passage of Front-Cover
Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the
Document already includes a cover text for the same cover, previously added by you or by arrangement made
by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on
explicit permission from the previous publisher that added the old one.
The author(s) and publisher(s) of the Document do not by this License give permission to use their names for
publicity for or to assert or imply endorsement of any Modified Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License, under the terms defined in
section 4 above for modified versions, provided that you include in the combination all of the Invariant Sections
of all of the original documents, unmodified, and list them all as Invariant Sections of your combined work in
its license notice, and that you preserve all their Warranty Disclaimers.
The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be
replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents,
make the title of each such section unique by adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number. Make the same adjustment to the section
titles in the list of Invariant Sections in the license notice of the combined work.
In the combination, you must combine any sections Entitled “History” in the various original documents, forming
one section Entitled “History”; likewise combine any sections Entitled “Acknowledgements”, and any sections
Entitled “Dedications”. You must delete all sections Entitled “Endorsements.”
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released under this License, and
replace the individual copies of this License in the various documents with a single copy that is included in the
collection, provided that you follow the rules of this License for verbatim copying of each of the documents in
all other respects.
You may extract a single document from such a collection, and distribute it individually under this License,
provided you insert a copy of this License into the extracted document, and follow this License in all other
respects regarding verbatim copying of that document.
7. AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other separate and independent documents or works, in
or on a volume of a storage or distribution medium, is called an “aggregate” if the copyright resulting from the
compilation is not used to limit the legal rights of the compilation’s users beyond what the individual works
Chapter 14: GNU Free Documentation License 97
permit. When the Document is included in an aggregate, this License does not apply to the other works in the
aggregate which are not themselves derivative works of the Document.
If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document
is less than one half of the entire aggregate, the Document’s Cover Texts may be placed on covers that bracket
the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form.
Otherwise they must appear on printed covers that bracket the whole aggregate.
8. TRANSLATION
Translation is considered a kind of modification, so you may distribute translations of the Document under
the terms of section 4. Replacing Invariant Sections with translations requires special permission from their
copyright holders, but you may include translations of some or all Invariant Sections in addition to the original
versions of these Invariant Sections. You may include a translation of this License, and all the license notices
in the Document, and any Warranty Disclaimers, provided that you also include the original English version of
this License and the original versions of those notices and disclaimers. In case of a disagreement between the
translation and the original version of this License or a notice or disclaimer, the original version will prevail.
If a section in the Document is Entitled “Acknowledgements”, “Dedications”, or “History”, the requirement
(section 4) to Preserve its Title (section 1) will typically require changing the actual title.
9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as expressly provided for under this
License. Any other attempt to copy, modify, sublicense or distribute the Document is void, and will automatically
terminate your rights under this License. However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such parties remain in full compliance.
10. FUTURE REVISIONS OF THIS LICENSE
The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from
time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to
address new problems or concerns. See http://www.gnu.org/copyleft/.
Each version of the License is given a distinguishing version number. If the Document specifies that a particular
numbered version of this License “or any later version” applies to it, you have the option of following the terms
and conditions either of that specified version or of any later version that has been published (not as a draft)
by the Free Software Foundation. If the Document does not specify a version number of this License, you may
choose any version ever published (not as a draft) by the Free Software Foundation.
Index
updated source files, recompiling . . . . . . . . . . . . . . . 9 ‘Wconversion’ option, warn about type conversions
Using GCC (Reference Manual) . . . . . . . . . . . . . . . 89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
‘Weffc++’ option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
‘Werror’ option, convert warnings to errors . . . . 27
V ‘Wimplicit’ option, warn about missing
‘v’ option, verbose compilation . . . . . . . . . . . . . . . . 62 declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
value, of macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 ‘Wold-style-cast’ option . . . . . . . . . . . . . . . . . . . . 49
variable shadowing . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 word-ordering, endianness . . . . . . . . . . . . . . . . . . . . 75
variable, warning of uninitialized use . . . . . . . . . . 46 word-size, determined from executable file . . . . . 75
variable-size arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 word-size, on UltraSPARC . . . . . . . . . . . . . . . . . . . 56
variables, in make. . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 ‘Wreturn-type’ option, warn about incorrect
vax, GNU C extension keyword . . . . . . . . . . . . . . . 21 return types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
verbose compilation, ‘-v’ option . . . . . . . . . . . . . . . 62 writable string constants, disabling . . . . . . . . . . . . 27
verbose help option . . . . . . . . . . . . . . . . . . . . . . . . . . 62 ‘Wshadow’ option, warn about shadowed variables
version number of GCC, displaying . . . . . . . . . . . . 62 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
virtual memory usage, limiting . . . . . . . . . . . . . . . . 64 ‘Wtraditional’ option, warn about traditional C
void return, incorrect use of . . . . . . . . . . . . . . . . . . 24 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
‘Wuninitialized’ option, warn about uninitialized
variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
W ‘Wunused’ option, unused variable warning . . . . . 24
‘Wwrite-strings’ option, warning for modified
‘W’ option, enable additional warnings . . . . . . . . . 25
string constants . . . . . . . . . . . . . . . . . . . . . . . . . 27
‘Wall’ option, enable common warnings . . . . . . . . . 5
warning option, ‘-W’ additional warnings . . . . . . . 25
warning options, ‘-Wall’ . . . . . . . . . . . . . . . . . . . . . . . 5
warning options, additional . . . . . . . . . . . . . . . . . . . 25
X
warning options, in detail . . . . . . . . . . . . . . . . . . . . 23 x86, floating-point arithmetic . . . . . . . . . . . . . . . . . 57
warning, format with different type arg . . . . . . . . . 6 x86, platform-specific options . . . . . . . . . . . . . . . . . 54
warnings, additional with ‘-W’ . . . . . . . . . . . . . . . . . 25 XL compilers, compatibility on AIX . . . . . . . . . . . 57
warnings, and optimization . . . . . . . . . . . . . . . . . . . 45 XOPEN extensions, GNU C Library . . . . . . . . . . 22
warnings, promoting to errors . . . . . . . . . . . . . . . . . 27
‘Wcast-qual’ option, warn about casts removing
qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Z
‘Wcomment’ option, warn about nested comments zero, division by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 zero, from underflow on DEC Alpha . . . . . . . . . . . 55