Chapter 3: Using the GNU Compiler Collection Features of GNU CC

Chapter 3: Using the GNU Compiler Collection Features of GNU CC

Chapter 3: Using the GNU Compiler Collection Page 1 of 21 Chapter 3: Using the GNU Compiler Collection In This Chapter l Features of GNU CC l Tutorial Example l Common Command-Line Options l Optimization Options l Debugging Options l Architecture-Specific Options l GNU C Extensions l PGCC: The Pentium Compiler GNU CC, more commonly known as GCC, is the GNU project’s compiler suite. It compiles programs written in C, C++, and Objective C. GCC speaks the various C dialects, such as ANSI C and traditional (Kernighan and Ritchie) C, fluently. It also compiles Fortran (under the auspices of g77). Front-ends for Pascal, Modula-3, Ada 9X, and other languages are in various stages of development. Because GCC is the cornerstone of almost all Linux development, I will discuss it in some depth. The examples in this chapter and throughout the book, unless noted otherwise, are based on GCC version 2.91.66. Note - If you kick around the Linux development community long enough, you will eventually hear or read about another compiler, egcs, the Experimental (or Enhanced) GNU Compiler Suite. egcs was intended to be a more actively developed and more efficient compiler than GCC. It was based on the GCC code base and closely tracked GCC releases. To make a long story short, in April, 1999, the Free Software Foundation, maintainers of GCC, appointed the egcs steering committee as GCC’s official maintainers. At the same time, GCC was renamed from the GNU C Compiler to the GNU Compiler Collection. In addition, the egcs and GCC code bases merged, ending a long fork in GCC’s code base and incorporating many bug fixes and enhancements. So, egcs and GCC are, for all intents and purposes, the same program. Features of GNU CC GCC gives the programmer extensive control over the compilation process. The compilation process includes up to four stages: 1. Preprocessing file://J:\MacmillanComputerPublishing\chapters\in201.html 3/22/01 Chapter 3: Using the GNU Compiler Collection Page 2 of 21 2. Compilation Proper 3. Assembly 4. Linking You can stop the process after any of these stages to examine or use the compiler’s output. You can control the amount and type of debugging information, if any, to embed in the resulting binary and, like most compilers, GCC can also perform code optimization. GCC allows you to mix debugging information and optimization. I strongly discourage doing so, however, because optimized code is hard to debug: static variables may vanish or loops may be unrolled, so that the optimized program does not correspond line-for-line with the original source code. GCC includes over 30 individual warnings and three general warning levels. GCC is also a cross- compiler, so you can develop code on one processor architecture that will be run on another. Finally, GCC sports a long list of extensions to C and C++. Most of these extensions enhance performance, assist the compiler’s efforts at code optimization, and make your job as a programmer easier. The price is portability, however. You will look at a few of the most common extensions because you will encounter them in the kernel header files, but I suggest you avoid them in your own code. Tutorial Example Before beginning an in-depth look at GCC, a short example will help you start using GCC productively right away. For the purposes of this tutorial, we will use the program in Listing 3.1. Listing 3.1 Program to Demonstrate GCC Usage /* * hello.c – Canonical "Hello, world!" program */ #include <stdio.h> int main(void) { printf("Hello, Linux programming world!\n"); return 0;} To compile and run this program, type $ gcc hello.c -o hello $ ./hello Hello, Linux programming world! The first command tells GCC to compile and link the source file hello.c, and create an executable name hello, specified using the -o argument. The second command executes the program, resulting in the output shown on the third line. The whole process is straightforward, but a lot took place under the hood that you did not see. GCC first ran hello.c through the preprocessor, cpp, to expand any macros and insert the contents of #included files. Next, it compiled the preprocessed source code to object code. Finally, the linker, ld, created the hello binary. Figure 3.1 depicts the compilation process graphically. file://J:\MacmillanComputerPublishing\chapters\in201.html 3/22/01 Chapter 3: Using the GNU Compiler Collection Page 3 of 21 Figure 3.1 The compilation process. You can re-create these steps manually, stepping through the compilation process. The first step is to run the preprocessor. To tell GCC to stop compilation after preprocessing, use GCC’s -E option: $ gcc -E hello.c -o hello.cpp Examine hello.cpp and you will see that the contents of stdio.h have indeed been inserted into the file, along with other preprocessing tokens. Figure 3.2 shows some of the contents of hello.cpp, starting at line 894. Note - The exact location of this text may vary slightly on your system. The next step is to compile hello.cpp to object code. Use GCC’s -c option to accomplish this: $ gcc -x cpp-output -c hello.cpp -o hello.o Figure 3.2 hello.c after preprocessing. In this case, you do not need to specify the name of the output file because the compiler creates an object filename by replacing .c with .o. The -x option tells GCC to begin compilation at the indicated step, in this case, with cpp-output, the preprocessed source code. How does GCC know how to deal with a particular kind of file? It relies upon file extensions to determine how to process a file correctly. The most common extensions and their interpretation are listed in Table 3.1. Table 3.1 How GCC Interprets Filename Extensions Extension Type .c C language source code .C, .cc C++ language source code .i Preprocessed C source code .ii Preprocessed C++ source code .S, .s Assembly language source code .o Compiled object code .a, .so Compiled library code file://J:\MacmillanComputerPublishing\chapters\in201.html 3/22/01 Chapter 3: Using the GNU Compiler Collection Page 4 of 21 Linking the object file, finally, creates a binary: $ gcc hello.o -o hello Hopefully, you will see that it is far simpler to use the abbreviated syntax I used above, gcc hello.c -o hello. The point of the step-by-step example was to demonstrate how to stop and start compilation at any step, should the need arise. One situation in which you would not want to complete the compilation process is when you are creating libraries. In this case, you only want to create object files, so the final link step is unnecessary. Another circumstance in which you would want to walk through the compilation process is when an #included file introduces conflicts with your own code or another #included file. Stepping through the process allows you to identify where the problem occurs and then to fix it. Being able to step through the process will make it clearer which file is introducing the conflict. Most C programs consist of multiple source code files, so each source file must be compiled to object code before the final link step. This requirement is easily met. Suppose, for example, that hello.c uses code from helper.c (see Listings 3.2 and 3.3). Listing 3.4 shows the source code for the modified hello program, howdy.c. Listing 3.2 Helper Code for howdy.c /* * helper.c – Helper code for howdy.c */ #include <stdio.h> void msg(void) { printf("This message sent from Jupiter.\n");} Listing 3.3 Header File for helper.c /* * helper.h – Header for helper.c */void msg(void) Listing 3.4 The Modified hello Program /* * howdy.c – Modifed "Hello, World!" program */ #include <stdio.h> #include "helper.h" int main(void) { printf("Hello, Linux programming world!\n"); msg(); return 0;} To compile howdy.c properly, use the following command line: $ gcc howdy.c helper.c -o howdy GCC goes through the same preprocess-compile-link steps as before. This time it creates object files for each source file, howdy.c and helper.c, before creating the binary, howdy, in the link stage. file://J:\MacmillanComputerPublishing\chapters\in201.html 3/22/01 Chapter 3: Using the GNU Compiler Collection Page 5 of 21 Typing long commands like this does become tedious. In Chapter 4, "Project Management Using GNU make," you learn how to solve this problem. The next section will begin introducing you to the multitude of GCC’s command-line options. Common Command-Line Options The list of command-line options GCC accepts runs to several pages, so Table 3.2 only lists the most common ones. Table 3.2 GCC Command-Line Options Option Description -o FILE Specifies the output filename; not necessary when compiling to object code. If FILE is not specified, the default name is a.out. -c Compiles without linking. -DFOO=BAR Defines a preprocessor macro named FOO with a value of BAR on the command line. -IDIRNAME Prepends DIRNAME to the list of directories searched for include files. -LDIRNAME Prepends DIRNAME to the list of directories that are searched for library files. -static Links against static libraries. By default, GCC links against shared libraries. -lFOO Links against libFOO. -g Includes standard debugging information in the binary. -ggdb Includes lots of debugging information in the binary that only the GNU debugger, gdb, can understand. -O Optimizes the compiled code. -ON Specifies an optimization level N, 0<=N<= 3.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    21 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us