logo资料库

An Introduction to GCC(英文版).pdf

第1页 / 共124页
第2页 / 共124页
第3页 / 共124页
第4页 / 共124页
第5页 / 共124页
第6页 / 共124页
第7页 / 共124页
第8页 / 共124页
资料共124页,剩余部分请下载后查看
An Introduction to GCC for the GNU Compilers gcc and g++ Brian Gough Foreword by Richard M. Stallman
A catalogue record for this book is available from the British Library. First printing, March 2004 (7/3/2004). Published by Network Theory Limited. 15 Royal Park Bristol BS8 3AL United Kingdom Email: info@network-theory.co.uk 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 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 Texinfo source for this manual may be obtained from: http://www.network-theory.co.uk/gcc/intro/src/ (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 A brief history of GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Major features of GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Programming in C and C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Conventions used in this manual. . . . . . . . . . . . . . . . . . . . . . . 5 2 Compiling a C program . . . . . . . . . . . . . . 7 2.1 Compiling a simple C program . . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 Finding errors in a simple program . . . . . . . . . . . . . . . . . . . . 8 2.3 Compiling multiple source files . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 Compiling files independently . . . . . . . . . . . . . . . . . . . . . . . . 10 2.4.1 Creating object files from source files . . . . . . . . 11 2.4.2 Creating executables from object files. . . . . . . . 11 2.4.3 Link order of object files. . . . . . . . . . . . . . . . . . . . 12 2.5 Recompiling and relinking . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.6 Linking with external libraries . . . . . . . . . . . . . . . . . . . . . . . 14 2.6.1 Link order of libraries . . . . . . . . . . . . . . . . . . . . . . 15 2.7 Using library header files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3 Compilation options . . . . . . . . . . . . . . . . . 19 3.1 Setting search paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.1.1 Search path example . . . . . . . . . . . . . . . . . . . . . . . 20 3.1.2 Environment variables. . . . . . . . . . . . . . . . . . . . . . 21 3.1.3 Extended search paths . . . . . . . . . . . . . . . . . . . . . 22 3.2 Shared libraries and static libraries . . . . . . . . . . . . . . . . . . . 23 3.3 C language standards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.3.1 ANSI/ISO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.3.2 Strict ANSI/ISO . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.3.3 Selecting specific standards . . . . . . . . . . . . . . . . . 28 3.4 Warning options in -Wall. . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.5 Additional warning options . . . . . . . . . . . . . . . . . . . . . . . . . . 30
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 Examining core files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.2 Displaying a backtrace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 6 Compiling with optimization . . . . . . . . . 45 6.1 Source-level optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 6.1.1 Common subexpression elimination. . . . . . . . . . 45 6.1.2 Function inlining. . . . . . . . . . . . . . . . . . . . . . . . . . . 46 6.2 Speed-space tradeoffs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.2.1 Loop unrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.3 Scheduling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.4 Optimization levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 6.5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 6.6 Optimization and debugging . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.7 Optimization and compiler warnings . . . . . . . . . . . . . . . . . . 53 7 Compiling a C++ program . . . . . . . . . . . 55 7.1 Compiling a simple C++ program . . . . . . . . . . . . . . . . . . . . . 55 7.2 Using the C++ standard library. . . . . . . . . . . . . . . . . . . . . . . 56 7.3 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 7.3.1 Using C++ standard library templates . . . . . . . 57 7.3.2 Providing your own templates. . . . . . . . . . . . . . . 58 7.3.3 Explicit template instantiation . . . . . . . . . . . . . . 60 7.3.4 The export keyword . . . . . . . . . . . . . . . . . . . . . . . 61 8 Platform-specific options . . . . . . . . . . . . 63 Intel and AMD x86 options . . . . . . . . . . . . . . . . . . . . . . . . . . 63 8.1 8.2 DEC Alpha options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.3 SPARC options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 8.4 POWER/PowerPC options . . . . . . . . . . . . . . . . . . . . . . . . . . 65 8.5 Multi-architecture support . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 9 Troubleshooting. . . . . . . . . . . . . . . . . . . . . 69 9.1 Help for command-line options . . . . . . . . . . . . . . . . . . . . . . . 69 9.2 Version numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 9.3 Verbose compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
iii 10 Compiler-related tools. . . . . . . . . . . . . . 73 10.1 Creating a library with the GNU archiver . . . . . . . . . . . . 73 10.2 Using the profiler gprof . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 10.3 Coverage testing with gcov . . . . . . . . . . . . . . . . . . . . . . . . . 77 11 How the compiler works . . . . . . . . . . . . 81 11.1 An overview of the compilation process . . . . . . . . . . . . . . 81 11.2 The preprocessor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 11.3 The compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 11.4 The assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 11.5 The linker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 12 Examining compiled files . . . . . . . . . . . 85 Identifying files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 12.1 12.2 Examining the symbol table . . . . . . . . . . . . . . . . . . . . . . . . 86 12.3 Finding dynamically linked libraries . . . . . . . . . . . . . . . . . 86 13 Getting help . . . . . . . . . . . . . . . . . . . . . . . 89 Further reading . . . . . . . . . . . . . . . . . . . . . . . . 91 Acknowledgements . . . . . . . . . . . . . . . . . . . . . 93 Other books from the publisher . . . . . . . . . 95 Free software organizations . . . . . . . . . . . . . 97 GNU Free Documentation License . . . . . . 99 ADDENDUM: How to use this License for your documents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
iv An Introduction to GCC
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 system for modern com- puters 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 com- pleted 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
分享到:
收藏