923 926




Linux Unleashed, Third Edition:Working with the Kernel





-->















Previous
Table of Contents
Next




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, 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:

Note:  When you are using the -o option, the executable file name must occur directly after the -o on the command line. Don’t put other options between the output name and the -o signal.



gcc -o count count.c


There are also compiler options that allow 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 multi-file C programs faster and easier to manage. Object code files that are created by gcchave 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 an .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 typically use are the -O and the -O2 options.
The -O option tells gcc to perform basic optimizations on the source code. In most cases, these optimizations make the code run faster. 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 typically results in code that executes more quickly.
In addition to the -O and -O2 optimization options, there are a number of lower level options that can be used to make the code faster. These options are very specific and should only be used if you fully understand the consequences that using these options will have on the compiled code. For a detailed description of these options, refer to the gcc man page.
Debugging and Profiling Options
The 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. With gcc you can use the -g option in conjunction with the -O option (which generates optimized code). This 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 some of the code that you have written will probably be changed by gcc when it optimizes it.
The -pg option tells gcc to add extra code to your program that will, when executed, generate profile information that can be used by the gprof program to display timing information about your program.
Debugging gcc Programs with gdb
Linux includes the GNU debugging program called gdb. The gdb debugger is a very powerful debugger that can be used to debug C and C++ programs. It allows you to see the internal structure or the memory that is being used by a program while it is executing. Some of the functions that gdb provides are

•  allows you to monitor the value of variables that are contained in your program
•  allows you to set breakpoints that will stop the program at a specific line of code
•  allows you to step through the code line by line

When you start gdb there are a number of options that you can specify on the command line. You will probably run gdb most often with this command:


gdb filename


When you invoke gdb in this way you are specifying the executable file that you want to debug. There are also ways of starting gdb that tell it to inspect a core file created by the executable file being examined or to attach gdb to a currently running process. To get a listing and brief description of each of these other options, you can 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 debugging information will be generated by the compiler. 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 debugger 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.
Summary
Recompiling kernel source code 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. From here, there are some other subjects you may want to check out. For example, to learn about


Using the C compiler that comes with most Linux distributions, see Chapter 26, “Programming in C.”
Ensuring that your system with the kernel is configured properly, see Part VI, starting with Chapter 32, “System Administration Basics.”
Using the source code control system to avoid too many files of slightly different contents littering your hard drive, see Chapter 56, “Source Code Control.”





Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
920 923
923 (2)
ReadMe (926)
926 (2)
873 923
ReadMe (923)

więcej podobnych podstron