[ Pobierz całość w formacie PDF ]
.This compiler was created under the Free Software Foundation's programming license and is therefore freely distributable.The GNU C Compiler that is packaged with the Slackware Linux distribution is a fully functional ANSI C compatible compiler.If you are familiar with a C compiler on a different operating system or hardware platform, you will be able to learn gcc very quickly.The GCC compiler is invoked by passing it a number of options and one or more filenames.The basic syntax for gcc is as follows:gcc [options] [filenames]The operations specified by the command line options are performed on each of the files that are on the command line.There are well over 100 compiler options that can be passed to gcc.You will probably never use most of these options, but you will use some of them on a regular basis.Compiler OptionsMany of the gcc options consist of more than one character.For this reason, you must specify each option with its own hyphen.You cannot group options after a single hyphen as you can with most Linux commands.For example, the following two commands are not the same:gcc -p -g test.cgcc -pg test.cThe first command tells gcc to compile test.c with profile information (-p) and also to store debugging information with the executable (-g).The second command just tells gcc to compile test.c with profile information for the gprof command (-pg).When you compile a program using gcc without any command line options, it creates an executable file (assuming that the compile was successful) and calls it a.out.To specify a name other than a.out for the executable file, you use the -o compiler option.For example, to compile a C program file named count.c into an executable file named count, use the following command:gcc -o count count.cAs shown in the preceding example, the executable file name must occur directly after the -o on the command line.Other compiler options enable you to specify how far you want the compile to proceed.The -c option tells gcc to compile the code into object code and to skip the assembly and linking stages of the compile.This option is used quite often because it makes the compilation of multifile C programs faster and easier to manage.Object code files that are created by gcc have a.o extension by default.The -S compiler option tells gcc to stop the compile after it has generated the assembler files for the C code.Assembler files that are generated by gcc have a.s extension by default.The -E option instructs the compiler to only perform the preprocessing compiler stage on the input files.When this option is used, the output from the preprocessor is sent to the standard output rather than being stored in a file.When you compile C code with gcc, it tries to compile the code in the least amount of time and also tries to create compiled code that is easy to debug.Making the code easy to debug means that the sequence of the compiled code is the same as the sequence of the source code and no code gets optimized out of the compile.There are many options that you can use to tell gcc to create smaller, faster executable programs at the cost of compile time and ease of debugging.Of these options, the two that you will use most are the -O and the -O2 options.The -O option tells gcc to perform basic optimizations on the source code.These optimizations make the code run faster in most cases.The -O2 option tells gcc to make the code as fast and small as it can.The -O2 option causes the compilation speed to be slower than it is when using the -O option, but it typically results in code that executes more quickly.In addition to the -O and -O2 optimization options, you can use a number of lower-level options to make the code faster.These options are very specific and should only be used if you fully understand the effect of these options on the compiled code.For a detailed description of these options, refer to the gcc man page.Debugging and Profiling OptionsThe gcc compiler supports several debugging and profiling options.Of these options, the two that you are most likely to use are the -g option and the -pg option.The -g option tells GCC to produce debugging information that the GNU debugger (gdb) can use to help you to debug your program.The gcc program provides a feature that many other C compilers do not have.With gcc, you can use the -g option in conjunction with the -O option (which generates optimized code).This feature can be very useful if you are trying to debug code that is as close as possible to what will exist in the final product.When you are using these two options together, be aware that gcc will probably change some of the code that you have written when gcc optimizes the code.The -pg option tells gcc to add extra code to your program that, when executed, generates profile information that the gprof program uses to display timing information about your program.Debugging gcc Programs with gdbLinux includes the GNU debugging program called gdb.You can use gdb debugger to debug C and C++ programs.It enables you to see the internal structure or the memory that a program is using while it is executing.This debugging program enables you to perform the following functions:Monitor the value of variables that your program containsSet breakpoints that stop the program at a specific line of codeStep through the code line by lineWhen you start gdb, you can specify a number of options on the command line.You will probably run gdb most often with this command:gdb filenameWhen you invoke gdb in this way, you are specifying the executable file that you want to debug.You can also tell gdb to inspect a core file that was created by the executable file being examined or attach gdb to a currently running process.To get a listing and brief description of each of these other options, refer to the gdb man page or type gdb -h at the command line.To get gdb to work properly, you must compile your programs so that the compiler generates debugging information.The debugging information that is generated contains the types for each of the variables in your program as well as the mapping between the addresses in the executable program and the line numbers in the source code.The gdb debugging program uses this information to relate the executable code to the source code.To compile a program with the debugging information turned on, use the -g compiler option.SummaryRecompiling the kernel source and adding new features to the kernel proceeds smoothly as long as you know what you are doing.Don't let the process scare you, but always keep boot disks on hand.Follow instructions wherever available as most new software has special requirements for linking into the kernel or replacing existing systems
[ Pobierz całość w formacie PDF ]