An Introduction to GCC
for the GNU Compilers gcc and g++ Revised and updated
Brian Gough Foreword by Richard M. Stallman
A catalogue record for this book is available from the British Library. Second printing, August 2005 (1/8/2005).
Revised and updated. First printing, March 2004 (7/3/2004).
Published by Network Theory Limited. 15 Royal Park Bristol BS8 3AL United Kingdom Email: [email protected] ISBN 0-9541617-9-3 Further information about this book is available from http://www.network-theory.co.uk/gcc/intro/ Cover Image: From a layout of a fast, energy-efficient hardware stack.(1) Image created with the free Electric VLSI design system by Steven Rubin of Static Free Software (www.staticfreesoft.com).
Static Free Software provides support for Electric to the electronics design industry. 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.”
The Essay on Theory Of Comparative Advantage
Theory of Comparative Advantage Theories of international trade originating from the English classical political economy have come through several stages of development. Their main subjects were and still remain the following questions: What is the basis of the international differentiation of labor? What international specialization is most effective and profitable for particular countries and ...
(1)
“A Fast and Energy-Efficient Stack” by J. Ebergen, D. Finchelstein, R. Kao, J. Lexau and R. Hopkins.
i
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 1.2 1.3 1.4 A brief history of GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Major features of GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Programming in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conventions used in this manual . . . . . . . . . . . . . . . . . . . . . . . . 3 4 4 5
2
Compiling a C program . . . . . . . . . . . . . . . . 7
2.1 2.2 2.3 2.4 Compiling a simple C program . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Finding errors in a simple program. . . . . . . . . . . . . . . . . . . . . . 8 Compiling multiple source files . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Compiling files independently . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.4.1 Creating object files from source files. . . . . . . . . 11 2.4.2 Creating executables from object files . . . . . . . . 11 Recompiling and relinking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 A simple makefile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Linking with external libraries . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.7.1 Link order of libraries. . . . . . . . . . . . . . . . . . . . . . . . 16 Using library header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.5 2.6 2.7 2.8
3
Compilation options . . . . . . . . . . . . . . . . . . 19
3.1 Setting search paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.1 Search path example . . . . . . . . . . . . . . . . . . . . . . . . . 3.1.2 Environment variables . . . . . . . . . . . . . . . . . . . . . . . 3.1.3 Extended search paths . . . . . . . . . . . . . . . . . . . . . . . 3.2 Shared libraries and static libraries . . . . . . . . . . . . . . . . . . . . 3.3 C language standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3.1 ANSI/ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3.2 Strict ANSI/ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3.3 Selecting specific standards . . . . . . . . . . . . . . . . . . 3.4 Warning options in -Wall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5 Additional warning options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.6 Recommended warning options . . . . . . . . . . . . . . . . . . . . . . . . 19 20 21 22 23 25 26 28 29 29 31 34
The Research paper on Library Management System 7
Introduction Library management system is an aspect or branch of information management system that keeps the records of both the library users, books in the library and the activities that is going on in the library, activities like borrowing books and returning books. Information management in general is the collection and management of information from one or more source and distribution of the ...
ii
An Introduction to GCC
4
Using the preprocessor . . . . . . . . . . . . . . . 35
4.1 Defining macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.2 Macros with values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4.3 Preprocessing source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5
Compiling for debugging . . . . . . . . . . . . . . 41
5.1 5.2 5.3 5.4 5.5 5.6 5.7 Examining core files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Displaying a backtrace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting a breakpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stepping through the program . . . . . . . . . . . . . . . . . . . . . . . . . Modifying variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Continuing execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . More information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 43 44 44 45 45 46
6
Compiling with optimization . . . . . . . . . 47
6.1 Source-level optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1.1 Common subexpression elimination . . . . . . . . . . 6.1.2 Function inlining . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Speed-space tradeoffs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2.1 Loop unrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3 Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.4 Optimization levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.6 Optimization and debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.7 Optimization and compiler warnings . . . . . . . . . . . . . . . . . . . 47 47 48 49 49 51 51 52 54 55
The Essay on Mood Effects on Fundamental Attribution Error
A series of experiments were conducted and found that several negative moods are responsible for decreasing the Fundamental Attribution Error, and positive moods are known to increase the FAE as it is influenced by the information processing consequences of the affective states. The initial experiment showed how happy mood was able to enhance and the sad mood reduce the dispositional attributions, ...
7
Compiling a C++ program . . . . . . . . . . . . 57
7.1 7.2 7.3 7.4 Compiling a simple C++ program . . . . . . . . . . . . . . . . . . . . . . C++ compilation options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the C++ standard library . . . . . . . . . . . . . . . . . . . . . . . . Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.1 Using C++ standard library templates . . . . . . . . 7.4.2 Providing your own templates. . . . . . . . . . . . . . . . 7.4.3 Explicit template instantiation . . . . . . . . . . . . . . . 7.4.4 The export keyword. . . . . . . . . . . . . . . . . . . . . . . . . 57 58 59 60 60 61 63 64
iii
8
Platform-specific options . . . . . . . . . . . . . 65
8.1 Intel and AMD x86 options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1.1 x86 extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1.2 x86 64-bit processors . . . . . . . . . . . . . . . . . . . . . . . . 8.2 DEC Alpha options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 SPARC options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.4 POWER/PowerPC options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.5 Multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.6 Floating-point issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.7 Portability of signed and unsigned types . . . . . . . . . . . . . . . 65 66 66 67 68 68 69 69 72
9
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . 75
9.1 9.2 9.3 9.4 9.5 Help for command-line options . . . . . . . . . . . . . . . . . . . . . . . . . Version numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Verbose compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stopping a program in an infinite loop . . . . . . . . . . . . . . . . . Preventing excessive memory usage . . . . . . . . . . . . . . . . . . . . 75 75 75 77 78
The Essay on GNU Public License and its Role
The LINUX software and other products are restricted with a License or patents to keep individual from altering or modifying the software. Developers usually release their software programs the way they think it best suit the potential business owner or users. The General Public License (GNU) role is to ensure when new software is released that it remains free to everyone to use as they please. ...
10
Compiler-related tools . . . . . . . . . . . . . . . 81
10.1 Creating a library with the GNU archiver . . . . . . . . . . . . . 81 10.2 Using the profiler gprof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 10.3 Coverage testing with gcov . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11
How the compiler works . . . . . . . . . . . . . 89
11.1 11.2 11.3 11.4 11.5 An overview of the compilation process . . . . . . . . . . . . . . . The preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 89 90 91 91
12
Examining compiled files . . . . . . . . . . . . 93
12.1 Identifying files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 12.2 Examining the symbol table . . . . . . . . . . . . . . . . . . . . . . . . . . 94 12.3 Finding dynamically linked libraries . . . . . . . . . . . . . . . . . . 94
13
Common error messages . . . . . . . . . . . . . 97
13.1 13.2 13.3 13.4 Preprocessor error messages . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Compiler error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Linker error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Runtime error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
iv
An Introduction to GCC
14
Getting help . . . . . . . . . . . . . . . . . . . . . . . . 109
Further reading . . . . . . . . . . . . . . . . . . . . . . . . . 111 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . 113 Other books from the publisher . . . . . . . . . 115 Free software organizations . . . . . . . . . . . . . 117 GNU Free Documentation License . . . . . . 119
ADDENDUM: How to use this License for your documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
The Term Paper on If Black English Isn't A Language Then Tell Me What Is?
The English language functioning as a system of racism and colonization in a “Post”-Colonial America. James Baldwin’s If Black English Isn’t Language, Then Tell Me, What Is asserts the English language as a contemporary system of racism and marginalization. The construction of Western language reflects the same alienating principles which validate the Western ideology of race, executing the ...
Foreword
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 addition 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
2
An Introduction to GCC
Chapter 1: Introduction
3
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 111).
The Term Paper on Operating Systems
An operating system is the program that manages all the application programs in a computer system. This also includes managing the input and output devices, and assigning system resources. Operating systems evolved as the solution to the problems that were evident in early computer systems, and coincide with the changing computer systems. Three cycles are clear in the evolution of computers, the ...
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.
1.1 A brief history of GCC
The original author of the GNU C Compiler (GCC) is Richard Stallman, the founder of the GNU Project. The GNU Project was started in 1984 to create a complete Unixlike operating system as free software, in order to promote freedom and cooperation among computer users and programmers. Every Unix-like operating system needs a C compiler, and as there were no free compilers in existence at that time, the GNU Project had to develop one from scratch. The work was funded by donations from individuals and companies to the Free Software Foundation, a non-profit organization set up to support the work of the GNU Project. The first release of GCC was made in 1987. This was a significant breakthrough, being the first portable ANSI C optimizing compiler released as free software. Since that time GCC has become one of the most important tools in the development of free software. A major revision of the compiler came with the 2.0 series in 1992, which added the ability to compile C++. In 1997 an experimental branch of the compiler (EGCS) was created, to improve optimization and C++ support. Following this work, EGCS was adopted as the new main-line of GCC development, and these features became widely available in the 3.0 release of GCC in 2001. Over time GCC has been extended to support many additional languages, including Fortran, ADA, Java and Objective-C. The acronym GCC is now used to refer to the “GNU Compiler Collection”. Its development is guided by the GCC Steering Committee, a group composed
4
An Introduction to GCC
of representatives from GCC user communities in industry, research and academia.
1.2 Major features of GCC
This section describes some of the most important features of GCC. First of all, GCC is a portable compiler—it runs on most platforms available today, and can produce output for many types of processors. In addition to the processors used in personal computers, it also supports microcontrollers, DSPs and 64-bit CPUs. GCC is not only a native compiler—it can also cross-compile any program, producing executable files for a different system from the one used by GCC itself. This allows software to be compiled for embedded systems which are not capable of running a compiler. GCC is 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.
1.3 Programming in C and C++
C and C++ are languages that allow direct access to the computer’s memory. Historically, they have been used for writing low-level systems soft(1)
For details see the license file ‘COPYING’ distributed with GCC.
Chapter 1: Introduction
5
ware, and applications where high-performance or control over resource usage are critical. However, great care is required to ensure that memory is accessed correctly, to avoid corrupting other data-structures. This book describes techniques that will help in detecting potential errors during compilation, but the risk in using languages like C or C++ can never be eliminated. In addition to C and C++ the GNU Project also provides other highlevel languages, such as GNU Common Lisp (gcl), GNU Smalltalk (gst), the GNU Scheme extension language (guile) and the GNU Compiler for Java (gcj).
These languages do not allow the user to access memory directly, eliminating the possibility of memory access errors. They are a safer alternative to C and C++ for many applications.
1.4 Conventions used in this manual
This manual contains many examples which can be typed at the keyboard. A command entered at the terminal is shown like this, $ command followed by its output. For example: $ echo “hello world” hello world The first character on the line is the terminal prompt, and should not be typed. The dollar sign ‘$’ is used as the standard prompt in this manual, although some systems may use a different character. 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 differences in the output on other systems. Some non-essential and verbose system-dependent output messages (such as very long system
(2)
See http://www.network-theory.co.uk/gcc/intro/
6
An Introduction to GCC
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.
Chapter 2: Compiling a C program
7
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.
2.1 Compiling a simple C program
The classic example program for the C language is Hello World. Here is the source code for our version of the program: #include int main (void) { printf (“Hello, world! “); 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 recommended 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++.
8
An Introduction to GCC
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.
2.2 Finding errors in a simple program
As mentioned above, compiler warnings are an essential aid when programming in C and C++. To demonstrate this, the program below contains a subtle error: it uses the function printf incorrectly, by specifying a floating-point format ‘%f’ for an integer value: #include int main (void) { printf (“Two plus two is %f “, 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 111).
Chapter 2: Compiling a C program
9
Without the warning option ‘-Wall’ the program appears to compile cleanly, but produces 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 warnings. 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.
2.3 Compiling multiple source files
A program can be split up into multiple files. This makes it easier to edit and understand, especially in the case of large programs—it also allows the individual parts to be compiled independently. In the following example we will split up the program Hello World into three files: ‘main.c’, ‘hello_fn.c’ and the header file ‘hello.h’. Here is the main function Program World Compiler">main program ‘main.c’: #include “hello.h” 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 declaration 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
10
An Introduction to GCC
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 #include “hello.h” void hello (const char * name) { printf (“Hello, %s! “, 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 is that the former searches for ‘FILE.h’ in the current directory before looking in the system header file directories. The include statement #include 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 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.
2.4 Compiling files independently
If a program is stored in a single file then any change to an individual function requires the whole program to be recompiled to produce a new
Chapter 2: Compiling a C program
11
executable. The recompilation of large source files can be very timeconsuming. When programs are stored in independent source files, only the files which have changed need to be recompiled after the source code has been modified. In this approach, the source files are compiled separately and then linked together—a two stage process. In the first stage, a file is compiled without creating an executable. The result is referred to as an object file, and has the extension ‘.o’ when using GCC. In the second stage, the object files are merged together by a separate program called the linker. The linker combines all the object files to create a single executable. An object file contains machine code where any references to the memory addresses of functions (or variables) in other files are left undefined. This allows source files to be compiled without direct reference to each other. The linker fills in these missing addresses when it produces the executable.
2.4.1 Creating object files from source files
The command-line option ‘-c’ is used to compile a source file to an object file. For example, the following command will compile the source file ‘main.c’ to an object file: $ gcc -Wall -c main.c This produces an object file ‘main.o’ containing the machine code for the main function. It contains a reference to the external function hello, but the corresponding memory address is left undefined in the object file at this stage (it will be filled in later by linking).
The corresponding command for compiling the hello function in the source file ‘hello_fn.c’ is: $ gcc -Wall -c hello_fn.c This produces the object file ‘hello_fn.o’. Note that there is no need to use the option ‘-o’ to specify the name of the output file in this case. When compiling with ‘-c’ the compiler automatically creates an object file whose name is the same as the source file, but with ‘.o’ instead of the original extension. There is no need to put the header file ‘hello.h’ on the command line, since it is automatically included by the #include statements in ‘main.c’ and ‘hello_fn.c’.
12
An Introduction to GCC
2.4.2 Creating executables from object files
The final step in creating an executable file is to use gcc to link the object files together and fill in the missing addresses of external functions. To link object files together, they are simply listed on the command line: $ gcc main.o hello_fn.o -o hello This is one of the few occasions where there is no need to use the ‘-Wall’ warning option, since the individual source files have already been successfully compiled to object code. Once the source files have been compiled, linking is an unambiguous process which either succeeds or fails (it fails only if there are references which cannot be resolved).
To perform the linking step gcc uses the linker ld, which is a separate program. On GNU systems the GNU linker, GNU ld, is used. Other systems may use the GNU linker with GCC, or may have their own linkers. The linker itself will be discussed later (see Chapter 11 [How the compiler works], page 89).
By running the linker, gcc creates an executable file from the object files. The resulting executable file can now be run: $ ./hello Hello, world! It produces the same output as the version of the program using a single source file in the previous section.
2.5 Recompiling and relinking
To show how source files can be compiled independently we will edit the main program ‘main.c’ and modify it to print a greeting to everyone instead of world: #include “hello.h” 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.
Chapter 2: Compiling a C program
13
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! 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.
2.6 A simple makefile
For those unfamiliar with make, this section provides a simple demonstration of its use. Make is a program in its own right and can be found on all Unix systems. To learn more about the GNU version of make you will need to consult the GNU Make manual by Richard M. Stallman and Roland McGrath (see [Further reading], page 111).
Make reads a description of a project from a makefile (by default, called ‘Makefile’ in the current directory).
A makefile specifies a set of compilation rules in terms of targets (such as executables) and their dependencies (such as object files and source files) in the following format: target : dependencies command For each target, make checks the modification time of the corresponding dependency files to determine whether the target needs to be rebuilt using the corresponding command. Note that the command lines in a makefile must be indented with a single TAB character, not spaces. GNU Make contains many default rules, referred to as implicit rules, to simplify the construction of makefiles. For example, these specify that ‘.o’ files can be obtained from ‘.c’ files by compilation, and that an executable can be made by linking together ‘.o’ files. Implicit rules are defined in terms of make variables, such as CC (the C compiler) and CFLAGS
(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.
14
An Introduction to GCC
(the compilation options for C programs), which can be set using VARIABLE =VALUE lines in the makefile. For C++ the equivalent variables are CXX and CXXFLAGS, while the make variable CPPFLAGS sets the preprocessor options. The implicit and user-defined rules are automatically chained together as necessary by GNU Make. A simple ‘Makefile’ for the project above can be written as follows: CC=gcc CFLAGS=-Wall main: main.o hello_fn.o 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 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
(2)
This assumes that there is no file called ‘clean’ in the current directory—see the discussion of “phony targets” in the GNU Make manual for details.
Chapter 2: Compiling a C program
15
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.
2.7 Linking with external libraries
A library is a collection of precompiled object files which can be linked into programs. The most common use of libraries is to provide system functions, such as the square root function sqrt found in the C math library. Libraries are typically stored in special archive files with the extension ‘.a’, referred to as static libraries. They are created from object files with a separate tool, the GNU archiver ar, and used by the linker to resolve references to functions at compile-time. We will see later how to create libraries using the ar command (see Chapter 10 [Compiler-related tools], page 81).
For simplicity, only static libraries are covered in this section— dynamic linking at runtime using shared libraries will be described in the next chapter. The standard system libraries are usually found in the directories ‘/usr/lib’ and ‘/lib’.(3) For example, the C math library is typically stored in the file ‘/usr/lib/libm.a’ on Unix-like systems. The corresponding prototype declarations for the functions in this library are 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 #include int main (void) { double x = sqrt (2.0); printf (“The square root of 2.0 is %f “, x); return 0; }
(3)
On systems supporting both 64 and 32-bit executables the 64-bit versions of the libraries will often be stored in ‘/usr/lib64’ and ‘/lib64’, with the 32-bit versions in ‘/usr/lib’ and ‘/lib’.
16
An Introduction to GCC
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, $ gcc -Wall calc.c -lm -o calc is equivalent to the original command above using the full library name ‘/usr/lib/libm.a’. In general, the compiler option ‘-lNAME ’ will attempt to link object files with a library file ‘libNAME.a’ in the standard library directories. Additional directories can specified with command-line options and environment variables, to be discussed shortly. A large program will typically use many ‘-l’ options to link libraries such as the math library, graphics libraries and networking libraries.
Chapter 2: Compiling a C program
17
2.7.1 Link order of libraries
The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. This means that a library containing the definition of a function should appear after any source files or object files which use it. This includes libraries specified with the short-cut ‘-l’ option, as shown in the following command: $ gcc -Wall calc.c -lm -o calc (correct order) With some linkers the opposite ordering (placing the ‘-lm’ option before the file which uses it) would result in an error, $ cc -Wall -lm calc.c -o calc (incorrect order) main.o: In function ‘main’: main.o(.text+0xf): undefined reference to ‘sqrt’ because there is no library or object file containing sqrt after ‘calc.c’. The option ‘-lm’ should appear after the file ‘calc.c’. When several libraries are being used, the same convention should be followed for the libraries themselves. A library which calls an external function defined in another library should appear before the library containing the function. For example, a program ‘data.c’ using the GNU Linear Programming library ‘libglpk.a’, which in turn uses the math library ‘libm.a’, should be compiled as, $ gcc -Wall data.c -lglpk -lm since the object files in ‘libglpk.a’ use functions defined in ‘libm.a’. Most current linkers will search all libraries, regardless of order, but since some do not do this it is best to follow the convention of ordering libraries from left to right. This is worth keeping in mind if you ever encounter unexpected problems with undefined references, and all the necessary libraries appear to be present on the command line.
2.8 Using library header files
When using a library it is essential to include the appropriate header files, in order to declare the function arguments and return values with the correct types. Without declarations, the arguments of a function can be passed with the wrong type, causing corrupted results. The following example shows another program which makes a function call to the C math library. In this case, the function pow is used to compute the cube of two (2 raised to the power of 3): #include
18
An Introduction to GCC
int main (void) { double x = pow (2.0, 3.0); printf (“Two cubed is %f “, 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.
Index
125
Index
#
#define, preprocessor directive . . . #elif, preprocessor directive . . . . . #else, preprocessor directive . . . . . #if, preprocessor directive . . . . . . . #ifdef, preprocessor directive . . . . #include, preprocessor directive . . #warning, preprocessor directive . . 35 98 98 29 35 10 98 option, enable debugging . . . . . 41 option, include path . . . . . . . . . 19 option, library search path . . . 19 option, linking with libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 ‘-lm’ option, link with math library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 ‘-m’ option, platform-specific settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ‘-m32’ and ‘-m64’ options, compile for 32 or 64-bit environment . . . . . 68 ‘-maltivec’ option, enables use of Altivec processor on PowerPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 ‘-march’ option, compile for specific CPU . . . . . . . . . . . . . . . . . . . . . . . . 65 ‘-mcmodel’ option, for AMD64 . . . . 66 ‘-mcpu’ option, compile for specific CPU . . . . . . . . . . . . . . . . . . . . . . . . 68 ‘-mfpmath’ option, for floating-point arithmetic . . . . . . . . . . . . . . . . . . . 66 ‘-mieee’ option, floating-point support on DEC Alpha . . . . . . 67 ‘-mminimal-toc’ option, on AIX . . 68 ‘-mno-fused-madd’ option, on PowerPC . . . . . . . . . . . . . . . . . . . . 68 ‘-msse’ and related options . . . . . . . 66 ‘-mtune’ option. . . . . . . . . . . . . . . . . . . 65 ‘-mxl-call’ option, compatibility with IBM XL compilers on AIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ‘-o’ option, set output filename . . . . 7 ‘-O0’ option, optimization level zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 ‘-O1’ option, optimization level one . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 ‘-O2’ option, optimization level two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 ‘-O3’ option, optimization level three . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 ‘-Os’ option, optimization for size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 ‘-pedantic’ option, conform to the ANSI standard (with ‘-ansi’) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 ‘-g’ ‘-I’ ‘-L’ ‘-l’
$
$, shell prompt . . . . . . . . . . . . . . . . . . . . 5
‘–help’ option, display command-line options. . . . . . . . 75 ‘–version’ option, display version number . . . . . . . . . . . . . . . . . . . . . . 75 ‘-ansi’ option, disable language extensions . . . . . . . . . . . . . . . . . . . 26 ‘-ansi’ option, used with g++. . . . . 57 ‘-c’ option, compile to object file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ‘-D’ option, define macro . . . . . . . . . 35 ‘-dM’ option, list predefined macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 ‘-E’ option, preprocess source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 ‘-fno-default-inline’ option. . . . 59 ‘-fno-implicit-templates’ option, disable implicit instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ‘-fprofile-arcs’ option, instrument branches. . . . . . . . . . . . . . . . . . . . . 86 ‘-fsigned-bitfields’ option . . . . . 74 ‘-fsigned-char’ option . . . . . . . . . . 73 ‘-ftest-coverage’ option, record coverage . . . . . . . . . . . . . . . . . . . . . 86 ‘-funroll-loops’ option, optimization by loop unrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 ‘-funsigned-bitfields’ option . . 74 ‘-funsigned-char’ option . . . . . . . . 73
126
‘-pg’ option, enable profiling . . . . . 84 ‘-pthread’ option, on AIX. . . . . . . . 69 ‘-rpath’ option, set run-time shared library search path . . . . . . . . . . 24 ‘-S’ option, create assembly code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 ‘-save-temps’ option, keeps intermediate files . . . . . . . . . . . . 39 ‘-static’ option, force static linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ‘-std’ option, select specific language standard . . . . . . . . . . . . . . . . . 26, 29 ‘-v’ option, verbose compilation . . 75 ‘-W’ option, enable additional warnings . . . . . . . . . . . . . . . . . . . . 31 ‘-Wall’ option, enable common warnings. . . . . . . . . . . . . . . . . . . . . . 7 ‘-Wcast-qual’ option, warn about casts removing qualifiers . . . . . 33 ‘-Wcomment’ option, warn about nested comments . . . . . . . . . . . . 29 ‘-Wconversion’ option, warn about type conversions . . . . . . . . . . . . . 31 ‘-Weffc++’ option . . . . . . . . . . . . . . . . 59 ‘-Werror’ option, convert warnings to errors . . . . . . . . . . . . . . . . . . . . . . . 34 ‘-Wformat’ option, warn about incorrect format strings . . . . . . 30 ‘-Wimplicit’ option, warn about missing declarations . . . . . . . . . 30 ‘-Wold-style-cast’ option . . . . . . . 59 ‘-Wreturn-type’ option, warn about incorrect return types . . . . . . . . 30 ‘-Wshadow’ option, warn about shadowed variables . . . . . . . . . . 33 ‘-Wtraditional’ option, warn about traditional C . . . . . . . . . . . . . . . . 34 ‘-Wuninitialized’ option, warn about uninitialized variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 ‘-Wunused’ option, unused variable warning . . . . . . . . . . . . . . . . . . . . . 30 ‘-Wwrite-strings’ option, warning for modified string constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
An Introduction to GCC
.c, C source file extension . . . . . . . . . 7 .cc, C++ file extension . . . . . . . . . . . 57 .cpp, C++ file extension . . . . . . . . . . 57 .cxx, C++ file extension . . . . . . . . . . 57 .h, header file extension . . . . . . . . . . . 9 .i, preprocessed file extension for C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 .ii, preprocessed file extension for C++ . . . . . . . . . . . . . . . . . . . . . . . . . 90 .o, object file extension . . . . . . . . . . 11 .s, assembly file extension . . . . . . . . 90 .so, shared object file extension . . 23
/
‘/tmp’ directory, temporary files . . 16
__gxx_personality_v0, undefined reference error . . . . . . . . . . . . . . . 58 _GNU_SOURCE macro, enables extensions to GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
6
64-bit platforms, additional library directories . . . . . . . . . . . . . . . . . . . 19 64-bit processor-specific options, AMD64 and Intel . . . . . . . . . . . . 66
A
a, archive file extension . . . . . . . . . . . 15 a.out, default executable filename ………………………… 7 ADA, gnat compiler . . . . . . . . . . . . . . . 3 additional warning options . . . . . . . 31 AIX, compatibility with IBM XL compilers . . . . . . . . . . . . . . . . . . . . 69 AIX, platform-specific options . . . . 68 AIX, TOC overflow error . . . . . . . . . 68 Alpha, platform-specific options . . 67 Altivec, on PowerPC . . . . . . . . . . . . . 68 AMD x86, platform-specific options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 AMD64, 64-bit processor specific options . . . . . . . . . . . . . . . . . . . . . . 66
.
.a, archive file extension . . . . . . . . . 15
Index
‘ansi’ option, disable language extensions . . . . . . . . . . . . . . . . . . . 26 ‘ansi’ option, used with g++ . . . . . . 57 ANSI standards for C/C++ languages, available as books . . . . . . . . . . 112 ANSI/ISO C, compared with GNU C extensions . . . . . . . . . . . . . . . . . . . 26 ANSI/ISO C, controlled with ‘-ansi’ option . . . . . . . . . . . . . . . . . . . . . . . 26 ANSI/ISO C, pedantic diagnostics option . . . . . . . . . . . . . . . . . . . . . . . 28 antiquated header in C++ . . . . . . . . . 98 ar, GNU archiver . . . . . . . . . . . . . 15, 81 archive file, .a extension . . . . . . . . . 15 archive file, explanation of . . . . . . . . 15 archiver, ar . . . . . . . . . . . . . . . . . . . . . . 89 arithmetic, floating-point . . . . . . . . . 69 ARM, multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 arrays, variable-size . . . . . . . . . . . . . . 28 asm extension keyword . . . . . . . . 27, 70 assembler, as. . . . . . . . . . . . . . . . . . . . . 89 assembler, converting assembly language to machine code . . . . 91 assignment discards qualifiers . . . 105 assignment of read-only location . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Athlon, platform-specific options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 attach, debug running program . . 77
127
branches, instrumenting for coverage testing. . . . . . . . . . . . . . . . . . . . . . . 86 break, command in gdb . . . . . . . . . . 44 breakpoints, defined . . . . . . . . . . . . . . 44 BSD extensions, GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 buffer, template example . . . . . . . . . 61 bug, example of . . . . . . . . . . . . 9, 18, 42 bus error . . . . . . . . . . . . . . . . . . . . . . . . 107
C
C include path . . . . . . . . . . . . . . . . . . . 21 C language, dialects of . . . . . . . . . . . 26 C language, further reading . . . . . 112 C library, standard . . . . . . . . . . 15, 112 C math library . . . . . . . . . . . . . . . . . . . 15 ‘c’ option, compile to object file . . 11 C programs, recompiling after modification . . . . . . . . . . . . . . . . . 12 C source file, .c extension . . . . . . . . . 7 C standard library . . . . . . . . . . . . . . . 15 C++ include path . . . . . . . . . . . . . . . . . 21 C++, compiling a simple program with g++. . . . . . . . . . . . . . . . . . . . . 57 C++, creating libraries with explicit instantiation . . . . . . . . . . . . . . . . . 64 C++, file extensions . . . . . . . . . . . . . . . 57 C++, g++ as a true compiler . . . . . . 57 C++, g++ compiler . . . . . . . . . . . . . . . . . 3 C++, instantiation of templates . . . 61 C++, namespace std . . . . . . . . . . . . . . 60 C++, standard library . . . . . . . . . 59, 61 C++, standard library templates . . 60 C++, templates . . . . . . . . . . . . . . . . . . . 60 c, C source file extension . . . . . . . . . . 7 C, compiling with gcc . . . . . . . . . . . . . 7 C, gcc compiler . . . . . . . . . . . . . . . . . . . 3 C/C++ languages, standards in printed form . . . . . . . . . . . . . . . . 112 C/C++, risks of using . . . . 4, 9, 18, 55 C_INCLUDE_PATH. . . . . . . . . . . . . . . . . . 21 c89/c99, selected with ‘-std’ . . . . . 29 cannot find library error . . . . . . 19, 21 cannot open shared object file . . . 23, 107 cast discards qualifiers from pointer target type . . . . . . . . . . . . . . . . . 105
B
backtrace, debugger command . . . 44 backtrace, displaying . . . . . . . . . . . . . 44 bash profile file. . . . . . . . . . . . . . . . . . . 43 bash profile file, login settings. . . . 21, 25 benchmarking, with time command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 big-endian, word-ordering . . . . . . . . 93 binary file, also called executable file ………………………… 7 Binutils, GNU Binary Tools . . . . . . 94 bitfields, portability of signed vs unsigned. . . . . . . . . . . . . . . . . . . . . 74 bits, 32 vs 64 on UltraSPARC . . . . 68 books, further reading . . . . . . . . . . . 112
128
casts, used to avoid conversion warnings . . . . . . . . . . . . . . . . . . . . 32 cc, C++ file extension . . . . . . . . . . . . 57 CC, make variable . . . . . . . . . . . . . . . . 13 CFLAGS, make variable . . . . . . . . . . . . 13 char, portability of signed vs unsigned. . . . . . . . . . . . . . . . . . . . . 72 character constant too long. . . . . . 101 circular buffer, template example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 COFF format . . . . . . . . . . . . . . . . . . . . 93 Collatz sequence . . . . . . . . . . . . . . . . . 83 collect2: ld returned 1 exit status . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 combined multiply and add instruction. . . . . . . . . . . . . . . . . . . 68 command, in makefile . . . . . . . . . . . . 13 command-line help option . . . . . . . . 75 ‘comment’ warning option, warn about nested comments . . . . . . 29 comments, nested . . . . . . . . . . . . . . . . 29 commercial support . . . . . . . . . . . . . 109 common error messages . . . . . . . . . . 97 common errors, not included with ‘-Wall’ . . . . . . . . . . . . . . . . . . . . . . 31 common subexpression elimination, optimization . . . . . . . . . . . . . . . . . 47 comparison of expression always true/false warning . . . . . . . . . . . 31 compilation, for debugging . . . . . . . 41 compilation, internal stages of . . . . 89 compilation, model for templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 compilation, options . . . . . . . . . . . . . . 19 compilation, stopping on warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 compile to object file, ‘-c’ option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 compiled files, examining . . . . . . . . . 93 compiler, converting source code to assembly code . . . . . . . . . . . . . . . 90 compiler, error messages. . . . . . . . . . 99 compiler, how it works internally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 compiler-related tools . . . . . . . . . . . . 81 compiling C programs with gcc . . . . 7 compiling C++ programs with g++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 compiling files independently . . . . . 10
An Introduction to GCC
compiling multiple files . . . . . . . . . . . . 9 compiling with optimization . . . . . . 47 configuration files for GCC . . . . . . . 75 const, warning about overriding by casts . . . . . . . . . . . . . . . . . . . . . . . . 33 constant strings, compile-time warnings . . . . . . . . . . . . . . . . . . . . 34 continue, command in gdb . . . . . . . 45 control reaches end of non-void function . . . . . . . . . . . . . . . . . . . . 104 control-C, interrupt . . . . . . . . . . . . . . 77 conventions, used in manual . . . . . . . 5 conversions between types, warning of . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 core file, examining . . . . . . . . . . . 41, 43 core file, not produced. . . . . . . . . . . . 42 coverage testing, with gcov . . . . . . . 85 CPLUS_INCLUDE_PATH . . . . . . . . . . . . . 21 cpp, C preprocessor . . . . . . . . . . . . . . 35 cpp, C++ file extension . . . . . . . . . . . 57 CPPFLAGS, make variable. . . . . . . . . . 13 ‘cr’ option, create/replace archive files . . . . . . . . . . . . . . . . . . . . . . . . . 82 crashes, saved in core file . . . . . . . . . 41 creating executable files from object files . . . . . . . . . . . . . . . . . . . . . . . . . 12 creating object files from source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 cxx, C++ file extension . . . . . . . . . . . 57 CXX, make variable . . . . . . . . . . . . . . . 13 CXXFLAGS, make variable. . . . . . . . . . 13
D
‘D’ option, define macro . . . . . . . . . . 35 data-flow analysis . . . . . . . . . . . . . . . . 55 DBM file, created with gdbm . . . . . . 20 debugging, compilation flags . . . . . . 41 debugging, with gdb . . . . . . . . . . . . . . 41 debugging, with optimization . . . . . 55 DEC Alpha, platform-specific options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 declaration, in header file . . . . . . . . . . 9 declaration, missing . . . . . . . . . . . . . . 17 default directories, linking and header files . . . . . . . . . . . . . . . . . . 19 default executable filename, a.out ………………………… 7
Index
default value, of macro defined with ‘-D’ . . . . . . . . . . . . . . . . . . . . . . . . . . 37 defining macros . . . . . . . . . . . . . . . . . . 35 denormalized numbers, on DEC Alpha . . . . . . . . . . . . . . . . . . . . . . . 67 dependencies, of shared libraries . . 94 dependency, in makefile . . . . . . . . . . 13 deployment, options for . . . 41, 52, 55 deprecated header in C++ . . . . . . . . 98 dereferencing pointer to incomplete type . . . . . . . . . . . . . . . . . . . . . . . . 102 dereferencing, null pointer . . . . . . . . 42 dialects of C language . . . . . . . . . . . . 26 different type arg, format warning ………………………… 8 disk space, reduced usage by shared libraries . . . . . . . . . . . . . . . . . . . . . 23 displaying a backtrace . . . . . . . . . . . . 44 division by zero . . . . . . . . . . . . . . . . . . 67 DLL (dynamically linked library), see shared libraries . . . . . . . . . . . . . . 23 ‘dM’ option, list predefined macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 dollar sign $, shell prompt . . . . . . . . . 5 double precision . . . . . . . . . . . . . . . . . . 69 dynamic loader. . . . . . . . . . . . . . . . . . . 23 dynamically linked libraries, examining with ldd . . . . . . . . . . 94 dynamically linked library, see shared libraries . . . . . . . . . . . . . . . . . . . . . 23
129
enhancements, to GCC . . . . . . . . . . 109 environment variables . . . . . . . . . . 5, 24 environment variables, extending an existing path . . . . . . . . . . . . . . . . 25 environment variables, for default search paths . . . . . . . . . . . . . . . . . 21 environment variables, setting permanently . . . . . . . . . . . . . . . . . 24 error messages, common examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 error while loading shared libraries . . . . . . . . . . . . . . . . . . . . . . . . 23, 107 error, undefined reference due to library link order . . . . . . . . . . . . 17 examining compiled files. . . . . . . . . . 93 examining core files . . . . . . . . . . . . . . 41 examples, conventions used in . . . . . 5 exception handling, floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 executable file . . . . . . . . . . . . . . . . . . . . . 7 executable, creating from object files by linking . . . . . . . . . . . . . . . . . . . 12 executable, default filename a.out ………………………… 7 executable, examining with file command . . . . . . . . . . . . . . . . . . . . 93 executable, running . . . . . . . . . . . . . . . 8 executable, symbol table stored in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 exit code, displayed in gdb . . . . . . . 46 explicit instantiation of templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 export keyword, not supported in GCC . . . . . . . . . . . . . . . . . . . . . . . . 64 extended precision, x86 processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 extended search paths, for include and link directories . . . . . . . . . . 22 extension, .a archive file . . . . . . . . . 15 extension, .c source file . . . . . . . . . . . 7 extension, .C, C++ file . . . . . . . . . . . . 57 extension, .cc, C++ file . . . . . . . . . . . 57 extension, .cpp, C++ file. . . . . . . . . . 57 extension, .cxx, C++ file. . . . . . . . . . 57 extension, .h header file . . . . . . . . . . . 9 extension, .i preprocessed file . . . . 90 extension, .ii preprocessed file . . . 90 extension, .o object file . . . . . . . . . . 11 extension, .s assembly file . . . . . . . . 90
E
‘E’ option, preprocess source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 ‘effc++’ warning option . . . . . . . . . . 59 EGCS (Experimental GNU Compiler Suite) . . . . . . . . . . . . . . . . . . . . . . . . 3 ELF format . . . . . . . . . . . . . . . . . . . . . . 93 elimination, of common subexpressions . . . . . . . . . . . . . . . 47 Emacs, gdb mode . . . . . . . . . . . . . . . . 46 embedded systems, cross-compilation for. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 empty macro, compared with undefined macro . . . . . . . . . . . . . 38 empty return, incorrect use of . . . 30 enable profiling, ‘-pg’ option . . . . . 84 endianness, word-ordering . . . . . . . . 93
130
extension, .so shared object file . . 23 external libraries, linking with . . . . 15
An Introduction to GCC
‘fpmath’ option, for floating-point arithmetic . . . . . . . . . . . . . . . . . . . 66 ‘fprofile-arcs’ option, instrument branches. . . . . . . . . . . . . . . . . . . . . 86 Free Software Foundation (FSF) . . . 3 FreeBSD, floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 ‘ftest-coverage’ option, record coverage . . . . . . . . . . . . . . . . . . . . . 86 function inlining, example of optimization . . . . . . . . . . . . . . . . . 48 function-call overhead . . . . . . . . . . . . 48 ‘funroll-loops’ option, optimization by loop unrolling . . . . . . . . . . . . 52 fused multiply and add instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
F
feature test macros, GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 features, of GCC . . . . . . . . . . . . . . . . . . 4 file command, for identifying files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 file extension, .a archive file . . . . . . 15 file extension, .c source file . . . . . . . . 7 file extension, .C, C++ file . . . . . . . . 57 file extension, .cc, C++ file . . . . . . . 57 file extension, .cpp, C++ file . . . . . . 57 file extension, .cxx, C++ file . . . . . . 57 file extension, .h header file . . . . . . . 9 file extension, .i preprocessed file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 file extension, .ii preprocessed file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 file extension, .o object file . . . . . . . 11 file extension, .s assembly file . . . . 90 file extension, .so shared object file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 file format not recognized . . . . . . . 106 file includes at least one deprecated or antiquated header . . . . . . . . . 98 file not recognized . . . . . . . . . . . . . . . 106 finish, command in gdb . . . . . . . . . 45 fldcw set floating point mode . . . . 70 floating point arithmetic, with SSE extensions . . . . . . . . . . . . . . . . . . . 66 floating point exception . . . . . . . . . 108 floating point exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 floating point exception, on DEC Alpha . . . . . . . . . . . . . . . . . . . . . . . 68 floating point, portability problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ‘fno-default-inline’ option . . . . . 59 ‘fno-implicit-templates’ option, disable implicit instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 format strings, incorrect usage warning . . . . . . . . . . . . . . . . . . . . . 30 format, different type arg warning ………………………… 8 Fortran, g77 compiler . . . . . . . . . . . . . 3
G
‘g’ option, enable debugging . . . . . . 41 g++, compiling C++ programs . . . . . 57 g++, GNU C++ Compiler . . . . . . . . . . 3 g77, Fortran compiler . . . . . . . . . . . . . 3 gcc, GNU C Compiler . . . . . . . . . . . . . 3 gcc, simple example . . . . . . . . . . . . . . . 7 gcc, used inconsistently with g++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 gcj, GNU Compiler for Java . . . . . . 3 gcov, GNU coverage testing tool . . 85 gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 gdb, debugging core file with . . . . . 43 gdb, Emacs mode . . . . . . . . . . . . . . . . 46 gdb, graphical interface . . . . . . . . . . . 46 gdbm, GNU DBM library . . . . . . . . . 20 generic programming, in C++ . . . . . 60 getting help . . . . . . . . . . . . . . . . . . . . . 109 gmon.out, data file for gprof . . . . . 85 gnat, GNU ADA compiler . . . . . . . . . 3 GNU archiver, ar . . . . . . . . . . . . . . . . 15 GNU C extensions, compared with ANSI/ISO C . . . . . . . . . . . . . . . . 26 GNU C Library Reference Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 GNU C Library, feature test macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 GNU Compilers, major features . . . 4 GNU Compilers, Reference Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 GNU debugger, gdb . . . . . . . . . . . . . . 41
Index
GNU GDB Manual . . . . . . . . . . . . . 111 GNU Make . . . . . . . . . . . . . . . . . . . . . . 13 GNU Make Manual . . . . . . . . . . . . . 111 GNU Press, manuals . . . . . . . . . . . . 111 GNU Project, history of . . . . . . . . . . . 3 GNU/Linux, floating-point arithmetic . . . . . . . . . . . . . . . . . . . 70 GNU_SOURCE macro (_GNU_SOURCE), enables extensions to GNU C Library . . . . . . . . . . . . . . . . . . . . . . 27 gnu89/gnu99, selected with ‘-std’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 gprof, GNU Profiler . . . . . . . . . . . . . 83 gradual underflow, on DEC Alpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 gxx_personality_v0, undefined reference error . . . . . . . . . . . . . . . 58
131
IEEE arithmetic standard, printed form . . . . . . . . . . . . . . . . . . . . . . . 112 IEEE options, on DEC Alpha . . . . 67 IEEE-754 standard . . . . . . . . . . . . . . 112 ii, preprocessed file extension for C++ . . . . . . . . . . . . . . . . . . . . . . . . . 90 illegal instruction error . . . . . . 66, 108 implicit declaration of function. . . 18, 30, 100 implicit rules, in makefile . . . . . . . . . 13 include guards, in header file . . . . . 62 include nested too deeply . . . . . . . . . 97 include path, extending with ‘-I’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 include path, setting with environment variables . . . . . . . . 21 inclusion compilation model, in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 independent compilation of files . . 10 Inf, infinity, on DEC Alpha . . . . . . . 67 infinite loop, stopping . . . . . . . . . . . . 77 initialization discards qualifiers . . 105 initialization makes integer from pointer without a cast . . . . . . 102 initializer element is not a constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 inlining, example of optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Insight, graphical interface for gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 instantiation, explicit vs implicit in C++ . . . . . . . . . . . . . . . . . . . . . . . . . 63 instantiation, of templates in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 instruction scheduling, optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 instrumented executable, for coverage testing. . . . . . . . . . . . . . . . . . . . . . . 86 instrumented executable, for profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Intel x86, platform-specific options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 intermediate files, keeping . . . . . . . . 39 invalid preprocessing directive . . . . 98 ISO C++, controlled with ‘-ansi’ option . . . . . . . . . . . . . . . . . . . . . . . 57 ISO C, compared with GNU C extensions . . . . . . . . . . . . . . . . . . . 26
H
h, header file extension . . . . . . . . . . . . 9 header file, .h extension . . . . . . . . . . . 9 header file, declarations in . . . . . . . . . 9 header file, default directories . . . . 19 header file, include path—extending with ‘-I’ . . . . . . . . . . . . . . . . . . . . 19 header file, missing . . . . . . . . . . . . . . . 17 header file, missing header causes implicit declaration . . . . . . . . . . 18 header file, not compiled . . . . . . . . . 11 header file, not found. . . . . . . . . . . . . 19 header file, with include guards . . . 62 header file, without .h extension for C++ . . . . . . . . . . . . . . . . . . . . . . . . . 60 Hello World program, in C . . . . . . . . 7 Hello World program, in C++ . . . . . 57 help options . . . . . . . . . . . . . . . . . . . . . . 75 history, of GCC . . . . . . . . . . . . . . . . . . . 3
I
‘I’ option, include path . . . . . . . . . . . 19 i, preprocessed file extension for C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 IBM XL compilers, compatibility on AIX . . . . . . . . . . . . . . . . . . . . . . . . . 69 identifying files, with file command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 IEEE arithmetic . . . . . . . . . . . . . . . . . 69
132
ISO C, controlled with ‘-ansi’ option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 ISO standards for C/C++ languages, available as books . . . . . . . . . . 112 iso9899:1990/iso9899:1999, selected with ‘-std’ . . . . . . . . . . 29 Itanium, multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
An Introduction to GCC
libraries, link error due to undefined reference . . . . . . . . . . . . . . . . . . . . 16 libraries, link order . . . . . . . . . . . . . . . 17 libraries, linking with . . . . . . . . . 15, 16 libraries, on 64-bit platforms . . . . . 19 libraries, stored in archive files. . . . 15 library header files, using . . . . . . . . . 17 library, C math library . . . . . . . . . . . 15 library, C standard library . . . . . . . . 15 library, C++ standard library . . . . . 59 libstdc++, C++ standard library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 line numbers, recorded in preprocessed files . . . . . . . . . . . . 38 link error, cannot find library . . . . . 19 link order, from left to right . . . . . . 17 link order, of libraries . . . . . . . . . . . . 17 link path, setting with environment variable . . . . . . . . . . . . . . . . . . . . . 22 linker, error messages . . . . . . . . . . . 106 linker, GNU compared with other linkers . . . . . . . . . . . . . . . . . . . . . . . 63 linker, initial description . . . . . . . . . 12 linker, ld . . . . . . . . . . . . . . . . . . . . . 89, 91 linking, creating executable files from object files. . . . . . . . . . . . . . . . . . . 12 linking, default directories . . . . . . . . 19 linking, dynamic (shared libraries) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 linking, explanation of. . . . . . . . . . . . 11 linking, undefined reference error due to library link order. . . . . . . . . . 17 linking, updated object files . . . . . . 12 linking, with external libraries . . . . 15 linking, with library using ‘-l’ . . . . 16 linkr error, cannot find library . . . . 19 Linux kernel, floating-point . . . . . . . 70 Lisp, compared with C/C++ . . . . . . . 4 little-endian, word-ordering . . . . . . . 93 loader configuration file, ld.so.conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 loader function . . . . . . . . . . . . . . . . . . . 23 login file, setting environment variables in . . . . . . . . . . . . . . . . . . 24 long double arithmetic . . . . . . . . . . . 72 loop unrolling, optimization. . . 49, 52 LSB, least significant byte . . . . . . . . 93
J
Java, compared with C/C++ . . . . . . . 4 Java, gcj compiler . . . . . . . . . . . . . . . . 3
K
K&R dialect of C, warnings of different behavior . . . . . . . . . . . . 34 kernel mode, on AMD64 . . . . . . . . . 67 Kernighan and Ritchie, The C Programming Language . . . . 112 key-value pairs, stored with GDBM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 keywords, additional in GNU C . . 26
L
‘L’ option, library search path . . . . 19 ‘l’ option, linking with libraries . . 16 language standards, selecting with ‘-std’ . . . . . . . . . . . . . . . . . . . . . . . 29 ld returned 1 exit status. . . . . . . . . 107 ld.so.conf, loader configuration file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ld: cannot find library error . . . . . . 19 LD_LIBRARY_PATH, shared library load path . . . . . . . . . . . . . . . . . . . . . . . . . 24 ldd, dynamical loader . . . . . . . . . . . . 94 levels of optimization . . . . . . . . . . . . . 51 libraries, creating with ar . . . . . . . . 81 libraries, creating with explicit instantiation in C++ . . . . . . . . . 64 libraries, error while loading shared library . . . . . . . . . . . . . . . . . . . . . . . 23 libraries, extending search path with ‘-L’ . . . . . . . . . . . . . . . . . . . . . . . . . . 19 libraries, finding shared library dependencies . . . . . . . . . . . . . . . . 94
Index
133
Motorola 680×0, word-order . . . . . . 93 MSB, most significant byte . . . . . . . 93 ‘msse’ and related options . . . . . . . . 66 ‘mtune’ option . . . . . . . . . . . . . . . . . . . . 65 multi-architecture support, discussion of . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 multiple directories, on include and link paths . . . . . . . . . . . . . . . . . . . 22 multiple files, compiling . . . . . . . . . . . 9 multiple-character character constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 multiply and add instruction . . . . . 68 multiply-defined symbol error, with C++ . . . . . . . . . . . . . . . . . . . . . . . . . 63 ‘mxl-call’ option, compatibility with IBM XL compilers on AIX . . . 69
M
‘m’ option, platform-specific settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ‘m32’ and ‘m64’ options, compile for 32 or 64-bit environment . . . . . 68 machine code. . . . . . . . . . . . . . . . . . . . . . 7 machine instruction, asm keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 machine-specific options . . . . . . . . . . 65 macro or ’#include’ recursion too deep . . . . . . . . . . . . . . . . . . . . . . . . . 97 macros, default value of . . . . . . . . . . 37 macros, defined with value . . . . . . . 36 macros, defining in preprocessor . . 35 macros, predefined . . . . . . . . . . . . . . . 36 major features, of GCC. . . . . . . . . . . . 4 major version number, of GCC . . . 75 makefile, example of . . . . . . . . . . . . . . 13 ‘maltivec’ option, enables use of Altivec processor on PowerPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 manuals, for GNU software . . . . . . 111 ‘march’ option, compile for specific CPU . . . . . . . . . . . . . . . . . . . . . . . . 65 math library . . . . . . . . . . . . . . . . . . . . . 15 math library, linking with ‘-lm’ . . . 16 ‘mcmodel’ option, for AMD64 . . . . . 66 ‘mcpu’ option, compile for specific CPU . . . . . . . . . . . . . . . . . . . . . . . . 68 memory usage, limiting. . . . . . . . . . . 78 ‘mfpmath’ option, for floating-point arithmetic . . . . . . . . . . . . . . . . . . . 66 ‘mieee’ option, floating-point support on DEC Alpha . . . . . . . . . . . . . . 67 minor version number, of GCC . . . 75 MIPS64, multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 missing header file, causes implicit declaration . . . . . . . . . . . . . . . . . . 18 missing header files . . . . . . . . . . . . . . . 17 missing prototypes warning . . . . . . . 30 ‘mminimal-toc’ option, on AIX . . . 68 MMX extensions . . . . . . . . . . . . . . . . . 66 ‘mno-fused-madd’ option, on PowerPC . . . . . . . . . . . . . . . . . . . . 68 modified source files, recompiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Motorola 680×0, floating-point arithmetic . . . . . . . . . . . . . . . . . . . 69
N
namespace std in C++ . . . . . . . . . . . . 60 namespace, reserved prefix for preprocessor . . . . . . . . . . . . . . . . . 36 NaN, not a number, on DEC Alpha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 native double-precision processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 nested comments, warning of . . . . . 29 NetBSD, floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 next, command in gdb . . . . . . . . . . . 44 nm command . . . . . . . . . . . . . . . . . . . . . 94 No such file or directory . . . . . 97, 107 No such file or directory, header file not found . . . . . . . . . . . . . . . . 19, 20 ‘no-default-inline’ option . . . . . . 59 null pointer . . . . . . . . . . . . . . . . . 42, 107 numerical differences . . . . . . . . . . . . . 69
O
‘O’ option, optimization level . . . . . 51 ‘o’ option, set output filename . . . . . 7 o, object file extension . . . . . . . . . . . 11 object file, .o extension . . . . . . . . . . 11 object file, creating from source using option ‘-c’ . . . . . . . . . . . . . . . . . . . 11 object file, examining with file command . . . . . . . . . . . . . . . . . . . . 93 object file, explanation of. . . . . . . . . 11
134
object files, linking to create executable file . . . . . . . . . . . . . . . 12 object files, relinking . . . . . . . . . . . . . 12 object files, temporary . . . . . . . . . . . 16 Objective-C . . . . . . . . . . . . . . . . . . . . . . . 3 old-style C++ header files . . . . . . . . . 98 ‘old-style-cast’ warning option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 OpenBSD, floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 optimization for size, ‘-Os’ . . . . . . . 52 optimization, and compiler warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 optimization, common subexpression elimination . . . . . . . . . . . . . . . . . . 47 optimization, compiling with ‘-O’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 optimization, example of . . . . . . . . . 52 optimization, explanation of . . . . . . 47 optimization, levels of . . . . . . . . . . . . 51 optimization, loop unrolling. . . 49, 52 optimization, speed-space tradeoffs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 optimization, with debugging . . . . . 55 options, compilation . . . . . . . . . . . . . . 19 options, platform-specific . . . . . . . . . 65 ordering of libraries . . . . . . . . . . . . . . 17 output file option, ‘-o’ . . . . . . . . . . . . . 7 overflow error, for TOC on AIX . . 68 overflow, floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 overhead, from function call . . . . . . 48
An Introduction to GCC
pipelining, explanation of. . . . . . . . . 51 platform-specific options . . . . . . . . . 65 POSIX extensions, GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 PowerPC and POWER, platform-specific options . . . . . 68 PowerPC64, multi-architecture support. . . . . . . . . . . . . . . . . . . . . . 69 precedence, when using preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 predefined macros . . . . . . . . . . . . . . . . 36 preprocessed files, keeping . . . . . . . . 39 preprocessing source files, ‘-E’ option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 preprocessor macros, default value of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 preprocessor, cpp . . . . . . . . . . . . . . . . . 89 preprocessor, error messages . . . . . . 97 preprocessor, first stage of compilation . . . . . . . . . . . . . . . . . . 90 preprocessor, using . . . . . . . . . . . . . . . 35 print debugger command . . . . . . . . 43 printf, example of error in format ………………………… 8 printf, incorrect usage warning . . 30 process id, finding . . . . . . . . . . . . . . . . 77 profile file, setting environment variables in . . . . . . . . . . . . . . . . . . 24 profiling, with gprof . . . . . . . . . . . . . 83 program crashes, saved in core file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 prototypes, missing. . . . . . . . . . . . . . . 30 ‘pthread’ option, on AIX . . . . . . . . . 69
P
parse error . . . . . . . . . . . . . . . . . . . . . . . 99 parse error at end of input . . . . . . . 99 parse error due to language extensions . . . . . . . . . . . . . . . . . . . 26 passing arg of function as another type to prototype . . . . . . . . . . . 105 patch level, of GCC . . . . . . . . . . . . . . 75 paths, extending environment variable . . . . . . . . . . . . . . . . . . . . . 25 paths, search . . . . . . . . . . . . . . . . . . . . . 19 ‘pedantic’ option. . . . . . . . . . . . . 26, 28 Pentium, platform-specific options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ‘pg’ option, enable profiling . . . . . . . 84
Q
qualifiers, warning about overriding by casts . . . . . . . . . . . . . . . . . . . . . 33 quotes, for defining empty macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
R
recompiling modified source files . . 12 red-zone, on AMD64 . . . . . . . . . . . . . 67 reference books . . . . . . . . . . . . . . . . . 112 reference, undefined due to missing library . . . . . . . . . . . . . . . . . . . . . . . 16 relinking updated object files . . . . . 12
Index
return discards qualifiers . . . . . . . . 105 return type, invalid. . . . . . . . . . . . . . . 30 Richard Stallman, principal author of GCC . . . . . . . . . . . . . . . . . . . . . . . . . 3 risks, examples of . . . . . . . . . . . . . . . 4, 9 rounding, floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 ‘rpath’ option, set run-time shared library search path . . . . . . . . . . 24 rules, in makefile . . . . . . . . . . . . . . . . . 13 run-time, measuring with time command . . . . . . . . . . . . . . . . . . . . 53 running an executable file, C . . . . . . 8 running an executable file, C++ . . . 57 runtime error messages . . . . . . . . . . 107
135
shell variables, setting permanently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 SIGINT signal . . . . . . . . . . . . . . . . . . . 77 signed bitfield option . . . . . . . . . . . . . 74 signed char option . . . . . . . . . . . . . . . 72 signed integer, casting . . . . . . . . . . . . 32 signed variable converted to unsigned, warning of . . . . . . . . . 31 SIGQUIT signal. . . . . . . . . . . . . . . . . . 78 simple C program, compiling . . . . . . 7 simple C++ program, compiling . . . 57 size, optimization for, ‘-Os’ . . . . . . . 52 Smalltalk, compared with C/C++ . . 4 so, shared object file extension . . . 23 soft underflow, on DEC Alpha . . . . 67 source code . . . . . . . . . . . . . . . . . . . . . . . 7 source files, recompiling . . . . . . . . . . 12 source-level optimization . . . . . . . . . 47 space vs speed, tradeoff in optimization . . . . . . . . . . . . . . . . . 49 SPARC, platform-specific options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Sparc64, multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 specs directory, compiler configuration files . . . . . . . . . . . . 75 speed-space tradeoffs, in optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 sqrt, example of linking with . . . . 15 SSE extensions . . . . . . . . . . . . . . . . . . . 66 SSE/SSE2 precision . . . . . . . . . . . . . . 72 stack backtrace, displaying . . . . . . . 44 stages of compilation, used internally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 standard library, C . . . . . . . . . . . . . . . 15 standard library, C++. . . . . . . . . . . . . 59 Standard Template Library (STL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 standards, C, C++ and IEEE arithmetic . . . . . . . . . . . . . . . . . . 112 static libraries . . . . . . . . . . . . . . . . . . . . 23 static linking, forcing with ‘-static’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 ‘static’ option, force static linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 std namespace in C++ . . . . . . . . . . . . 60 ‘std’ option, select specific language standard . . . . . . . . . . . . . . . . . 26, 29 step, command in gdb . . . . . . . . . . . 44
S
‘S’ option, create assembly code . . 90 s, assembly file extension . . . . . . . . . 90 ‘save-temps’ option, keeps intermediate files . . . . . . . . . . . . 39 scanf, incorrect usage warning. . . 30, 107 scheduling, stage of optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Scheme, compared with C/C++ . . . . 4 search paths . . . . . . . . . . . . . . . . . . . . . 19 search paths, example . . . . . . . . . . . . 20 search paths, extended . . . . . . . . . . . 22 segmentation fault . . . . . . . . . . . 42, 107 selecting specific language standards, with ‘-std’ . . . . . . . . . . . . . . . . . . 29 separator, in makefiles . . . . . . . . . . . . 13 set, command in gdb. . . . . . . . . . . . . 45 shadowing of variables. . . . . . . . . . . . 33 shared libraries . . . . . . . . . . . . . . . . . . . 23 shared libraries, advantages of . . . . 23 shared libraries, dependencies . . . . 94 shared libraries, error while loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 shared libraries, examining with ldd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 shared libraries, setting load path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 shared object file, .so extension . . 23 shell prompt . . . . . . . . . . . . . . . . . . . . . . 5 shell quoting . . . . . . . . . . . . . . . . 38, 111 shell variables . . . . . . . . . . . . . . 5, 21, 24
136
stopping execution, with breakpoints in gdb . . . . . . . . . . . . . . . . . . . . . . . 44 strict ANSI/ISO C, ‘-pedantic’ option . . . . . . . . . . . . . . . . . . . . . . . 28 strip command . . . . . . . . . . . . . . . . . . 94 subexpression elimination, optimization . . . . . . . . . . . . . . . . . 47 suggest parentheses around assignment used as truth value . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Sun SPARC, platform-specific options . . . . . . . . . . . . . . . . . . . . . . 68 support, commercial . . . . . . . . . . . . 109 SVID extensions, GNU C Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 symbol table . . . . . . . . . . . . . . . . . . . . . 41 symbol table, examining with nm . . 94 syntax error . . . . . . . . . . . . . . . . . . . . . . 99 system libraries . . . . . . . . . . . . . . . . . . 15 system libraries, location of . . 15, 19, 69 system-specific predefined macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 SYSV, System V executable format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
An Introduction to GCC
time command, measuring run-time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 TOC overflow error, on AIX . . . . . . 68 tools, compiler-related . . . . . . . . . . . . 81 tradeoffs, between speed and space in optimization . . . . . . . . . . . . . . . . . 49 Traditional C (K&R), warnings of different behavior . . . . . . . . . . . . 34 translators, from C++ to C, compared with g++. . . . . . . . . . . . . . . . . . . . . 57 troubleshooting options . . . . . . . . . . 75 ‘tune’ machine-specific option . . . . 65 type conversions, warning of . . . . . . 31 typeof, GNU C extension keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
U
ulimit command . . . . . . . . . . . . . 42, 78 UltraSPARC, 32-bit mode vs 64-bit mode, . . . . . . . . . . . . . . . . . . . . . . . 68 undeclared identifier error for C library, when using ‘-ansi’ option . . . . . . . . . . . . . . . . . . . . . . . 27 undeclared variable. . . . . . . . . . . . . . . 99 undefined macro, compared with empty macro . . . . . . . . . . . . . . . . 38 undefined reference error . . . . . 16, 17, 107 undefined reference error for __gxx_personality_v0 . . . . . . 58 undefined reference to ’main’ . . . . 107 undefined reference to C++ function, due to linking with gcc . . . . . . 58 underflow, floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 underflow, on DEC Alpha . . . . . . . . 67 uninitialized pointer . . . . . . . . . . . . . 107 uninitialized variable, warning of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 unix, GNU C extension keyword . . 27 unknown escape sequence . . . . . . . 103 unoptimized code (‘-O0’) . . . . . . . . . 51 unrolling, of loops (optimization) . . . . . . . . . . . . . . . . . . . . . . . . . . 49, 52 unsigned bitfield option . . . . . . . . . . 74 unsigned char option . . . . . . . . . . . . . 72 unsigned integer, casting . . . . . . . . . 32
T
‘t’ option, archive table of contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 tab, in makefiles. . . . . . . . . . . . . . . . . . 13 table of contents, in ar archive . . . 82 table of contents, overflow error on AIX . . . . . . . . . . . . . . . . . . . . . . . . . 68 target, in makefile . . . . . . . . . . . . . . . . 13 tcsh, limit command . . . . . . . . . . . . . 42 templates, explicit instantiation . . 63 templates, export keyword . . . . . . . 64 templates, in C++ . . . . . . . . . . . . . . . . 60 templates, inclusion compilation model . . . . . . . . . . . . . . . . . . . . . . . 61 temporary files, keeping . . . . . . . . . . 39 temporary files, written to ‘/tmp’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 termination, abnormal (core dumped) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 threads, on AIX . . . . . . . . . . . . . . . . . . 69 Thumb, alternative code format on ARM . . . . . . . . . . . . . . . . . . . . . . . . 69