Article Source: Network
There is a lot of software development tools in the release of Linux. Many of them are developed for C and C applications. This article describes tools that can be used for C applications and debugging under Linux. The main purpose of this article describes how to use C compilers and other C programming tools under Linux, not C language programming tutorials. In this article, you will learn the following: · What is a C · GNU C compiler · Use GDB to debug GCC applications You can also see other useful C programming tools issued by Linux. These tools include PRETTY Print Programs, additional debugging tools, Automatic Function Prototypers. Note: The PRETTY Print Programs automatically helps you format the source code to generate a consistent indentation format. What is C? C is a widely used universal programming language in the early days of UNIX operating systems. It was first written by Dennis Ritchie of Bell Lab for UNIX, and UNIX was written in assembly language and a language called B. From then on, C became the most widely used computer language in the world. C can get such a wide support in the programming field: • It is a very common language. Almost you can think of at least one of the available C compilers. And its grammar and function library are unified on different platforms. This feature is very attractive to developers. · The speed of writing with C is very fast. · C is a system language on all versions of UNIX. C has a great development in the past twenty years. A C language standard called ANSI C is released in the Final At the end of the 1980s. The American National Standards Institute is released. This ensures the consistency of C in the future in different platforms. In the 1980s, an object-oriented extension called C . C will be described in another article "C Programming". The C compiler available on Linux is a GNU C compiler, which is built on the program license of the Free Software Foundation, so it can be freely released. You can find it on Linux's distribution disc. The GNU C compiler is a full-featured ANSI C compatible compiler with the Slackware Linux. The GNU C compiler (GCC). If you are familiar with a C compiler on other operating systems or hardware platforms, you will be able to master GCC quickly. This section describes how to use the GCC and some of the most common options for some GCC compilers. Using GCC usually follows some options and file names using the GCC compiler. The basic usage of the GCC command is as follows: GCC [Options] [filenames] command line option specifies the action performed on the command line. The next section will narrate some options you will use. The GCC option GCC has more than 100 compilation options available. Many of these options you may never use, but some main options will be used frequently. Many GCC options include more than one character. So you must specify your respective hyphens for each option, just like most Linux commands, you can't follow a set of options after a separate hyphen. For example, the two of the following commands are different: gcc -p -g test.c gcc -pg test.c The first command tells the GCC to establish an analysis (PROF command) when compiling Test.c and add debugging information. Go to the executable file. The second command only tells the GCC to establish a profiling information for the gprof command. When you don't have any options to compile a program, GCC will establish (assuming compile success) a executable name called A.out. For example, the following command will generate a file called A.out in the current directory: GCC Test.c You can use the -o compilation option to specify a file name for the generated executable to replace A.out.
For example, compiling a COUNT.C's C program as a executable called count, you will enter the following command: gcc -o count country.c Note: When you use the -o option, the -o must follow A file name. The GCC also has the compilation option for specifying the compiler processing. The -c option tells the GCC to skip the assembly and connection of the source code as a target code. This option is used very frequently because it makes it easier when compiling multiple C programs faster and easier to manage. The target code file established by the GCC has a .o extension. The -s compilation option tells GCC to stop compiling after the assembly language file is generated for C code. The default extension of the assembly language file generated by the GCC is .S. The -e option indicates that the compiler is only preprocessing the input file. When this option is used, the output of the preprocessor is sent to the standard output instead of being stored in the file. Optimization Option When you compile C code with GCC, it will try to complete compilation with the least amount of time and make the compiled code easy to debug. Easy to debug means that the compiled code is the same as the source code, and the compiled code has not been optimized. There are many options that can be used to tell GCC to generate smaller and faster executables on the basis of consuming more compile time and sacrifice. The most typical in these options is -O and -O2 options. The -o option tells the GCC to optimize the source code. These optimizations will make the program faster in most cases. -O2 option tells GCC to generate as small and as fast as possible. -O2 option will slow compilation speed than using -O -O. But usually generated code execution speed will be faster. In addition to the -O and -O2 optimization options, there are some low-level options to produce faster code. These options are very special, and it is best to use when you fully understand these options will be used when the compiled code is generated. Detailed description of these options, please refer to the GCC's guide page, type the Man GCC on the command line. The debug and parsing options GCC support several commissioning and parsing options. The -g and -pg options are most common in these options. The -g option tells the GCC to generate debugging information that can be used by the GNU debugger to debug your program. GCC provides a lot of features in many other C compilers that you can use in the GCC you can use. This is very useful because you can debug your code with the final product as possible. When you use these two options at the same time, you must know that some code you have written is already modified by GCC when optimized. For more information on debugging C procedures, please see the "GDB debug C program". -pg Option Tell GCC to add additional code in your program, execute, generate the profiling information of Gprof to display the time consumption of your program. For more information on GPROF, please refer to the "GPROF" section. Using the GDB debug GCC program Linux contains a GNU debugger called GDB. GDB is a powerful debugger for debugging C and C programs. It allows you to observe the internal structure and memory usage of the program at runtime. The following is some of the features provided by GDB: · It allows you to monitor the value of variables in your program. · It allows you to set breakpoints to stop execution on the specified code line. · It makes you execute your code a line.
Type GDB on the command line and press Enter to run GDB. If everything is normal, GDB will be started and you will see similar content on the screen: GDB is Free Software and you are Welcome to Distribute Copies of IT Under Certain Conditions; Type "Show Copying" to see the conditions. There is Absolutely No Warranty for GDB; Type "Show Warranty" for Details. GDB 4.14 (i486-SLAKWARE-Linux), Copyright 1995 Free Software Foundation, Inc. (GDB) When you start GDB, you can specify on the command line. A lot of options. You can also run GDB: GDB as you can run GDB in this way, you can specify the programs you want to debug. This will tell GDB to load an executable named FNAME. You can also use GDB to check an Core file that is generated by an exception, or connects with a running program. You can refer to the GDB guide page or type GDB -H on the command line to get a simple list of instructions on these options. To debug code for debugging, in order to make GDB work normally, you must make your program contain debug information when compiling. The debugging information contains the type of each variable in your program and the number of address mappings in the executable and the line number of the source code. GDB uses this information to associate the source code and the machine code. Turn on the debug option with the -g option when compiling. GDB basic command GDB supports a lot of commands to make you can achieve different features. These commands are loaded from simple files to the complex commands that allow you to check the call content called, Table 27.1 lists some of the commands you use when you use GDB debugging. For more information on GDB, please refer to the GDB guide page. Table 27.1. Basic GDB Command. Command Description File loads the executable you want to debug. Kill terminates the program being debugging. List executes a source code but does not enter the inside of the function. NEXT executes a source code but does not enter the inside of the function. STEP performs a row source code and enters the inside of the function. Run executes the current debugging program Quit Termination GDB Watch allows you to monitor the value of a variable regardless of whether it is changed. Break sets breakpoints in the code, which will hang it when the program is executed here. Make allows you to re-generate executables without exiting GDB. The shell allows you to execute the UNIX shell command without leaving GDB. GDB supports a lot of command editing features like UNIX shell programs. You can press the Tab button like it in Bash or TCSH to make GDB to help you fill a unique command, if the GDB will list all the matching commands if the GDB will list all match. You can also flip history commands with cursor keys. GDB Application Example This section teaches you step by step by step by step. The debugged program is quite simple, but it shows the typical application of GDB. The program that will be debugged will be listed below. This program is called Greeting, which shows a simple greeting and listed it in the back sequence.
#include main () {char my_string [] = "hello there"; my_print (my_string); my_print2 (my_string);} void my_print (char * string) {Printf ("THE STRING IS% S / N", String); } void my_print2 (char * string) {char * string2; int size, i; size = strlen (string); string2 = (char *) malloc (size 1); for (i = 0; i The output of the first time to type the list command is as follows: 1 #include 2 3 main () 4 {5 char my_string [] = "hello there"; 6 7 my_print (my_string); 8 my_print2 (my_string); 9} 10 If you press Enter, GDB will execute a list command again, give the following output: 11 my_print (char * string) 12 {13 printf ("THE STRING IS% S / N", String); 14} 15 16 my_print2 (char * string 17 {18 char * STRING2; 19 int size, i; 20, pressing a cycle of a Greeting program: 21 size = strlish (string); 22 string2 = (char *) Malloc (size 1) ; 23 for (i = 0; i This explains why there is no output when printing String 2. Now find out where the problem is, correct this error is very easy. You have to change the offset of the first character written in the code to SIZE - 1 instead of size. This is because String2 is 12, but the start offset is 0, the character in the string from the offset 0 to the offset amount 10, the offset amount 11 is empty characters. In order to make the code work properly, there are many modifications. One is a variable that is another actual size of the actual size than the string. This is the code of this solution: #include main () {char my_string [] = "hello there"; my_print (my_string); my_print2;} my_print (char * string) {printf ("the string is% S / N ", string);} my_print2 (char * string) {char * string2; int size, size2, i; size = strlen (string); size2 = size -1; string2 = (char *) Malloc (size 1); for (i = 0; i When Calls prints a tracking result, it gives the file name of the file in the function behind the function: main [test.c] If the function is not a file given to the call, Calls does not know the call Where is the function from, only the name of the function is displayed: Printf calls does not output the recursive and static function. The recursive function is displayed as the following: FACT <<< Recursive In factorial.c >>> Static function like this: Total [static in caverculate.c] As an example, assume that the following programs are processed using the Calls: #include main ) {char my_string [] = "hello there"; my_print (my_String); my_print2;} my_print (char * string) {printf ("THE STRING IS% S / N", String);} my_print2 (char * {char * string2; int size, size2, i; size = strlen (string); size2 = size -1; string2 = (char *) Malloc (size 1); for (i = 0; i If you let CProto processes the following code: #include main () {char my_string [] = "Hello there"; my_print (my_string); my_print2 (my_string);} my_print (char * string) {printf ("THE STRING IS% S / N ", * String);} my_print2 (char * string) {char * string2; int size, size2, i; size = strlen (string); size2 = size -1; string2 = (char *) malloc (size) Malloc (size) Malloc (size); SIZE 1); for (i = 0; i The following example is Indent's default output: running previous C code: #include main () {char my_string [] = "hello there"; my_print (my_string); my_print2 (my_String);} my_print (char * string) {Printf ("THE STRING IS% S / N", * String);} my_print2 (char * string) {char * string2; int size, size2, i; size = strlen (string); SIZE2 = Size -1; string2 = (char *) malloc (size 1); for (i = 0; i