The main purpose of this article describes how to use C compilers and other C programming tools under Linux, not C language programming tutorial. In this article, you will learn the following knowledge: What is the C GNU C compiler with 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: PRETTY Print Programs Automatically helps you format the source code to generate consistent indentation format ------------------------------------- ------------------------------ What is C? C is a widely used in the early days of UNIX operating systems Generic programming language. It was first written by Dennis Ritchie, Bell Lab, for UNIX's auxiliary development. Unix is written in assembly language and a language called B. From then on, c will become the world. Use the most widely used computer language. C can get such a wide support in the programming field: it is a very common language. Almost at least one of the computers you can think of it can be used. The C compiler. And its grammar and function library are unified on different platforms. This feature is very attractive to developers. The execution speed of writing with C is very fast. C is all version of UNIX System language. C has a great development in the past 20 years. A C language standard called ANSI C is released in the US National Standards Institute in the late 1980s. This is more assurance. In the future, C is consistent on different platforms. In the 1980s, a C . C will be in another article "C programming" in another article.
Described. The C compiler available on Linux is a GNU C compiler. It is based on the programming license of the Free Software Foundation, so you can freely release it. You can find it on Linux's release disc. GNU C compile The GNU C compiler (GCC) issued by Slackware Linux is a full-featured ANSI C compatible compiler. 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 GCC compilers. Use GCC usually followed some options and file names. The basic usage of the GCC command is as follows: GCC [options] [filenames] command line option specified The operation will be performed on each page given on the command line. The next section will narrate some options you will use. The GCC option GCC has more than 100 compile options available. Many of these options you may always Will not use, but some major options will be used frequently. Many GCC options include more than one character. So you must specify your respective characters for each option, and you can't like most Linux commands. With a separate hyphen followed by an option. For example, the following two commands are different: gcc -p -g test.c gcc -pg test.c The first command tells the GCC to compile Test.c for the PROF command Establish a profile information and add the debug information 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 compilation success A executable file named A.,, 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 generate executable files Specifies a file name instead of A.out. For example, compile a C program called Count.c into a executable called count, you will enter the following command: gcc -o count count.c ----- -------------------------------------------------- ------------------------- Note:
When you use the -o option, the -o must follow a file name. --------------------------------- ---------------------------------------------- GCC is also Specifies how much compiler processing. The -c option tells the GCC to skip the assembly and connection only to compile the source code as a target code. This option is very frequent because it makes compiling multiple C procedures faster and It is more likely to manage. The target code file established by the default is a. -S compilation option tells the GCC to stop compilation. The default expansion of the assembly language file generated by the GCC is generated for C code. The name is .s. -E option indicates that the compiler is only preproces 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 with GCC When C code, it will try to complete compilation with the least amount of time and easy to debug. Easy to debug means that the compiled code is the same as the source code, and the compiled code is not optimized. There are many options Can be used to tell GCC to generate a smaller and faster executable on the basis of more compile time and sacrificial easily debugging. These options are -o and -o2 options. -O Option tells GCC to source code Basic optimization. These optimizations will make the program execute faster in most cases. -O2 option tells GCC to generate as small and as fast as possible. The -O2 option will make compiled speeds slower than using -O. But the usual 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 fully understand these options. Will use what kind of effect will be used when the compiled code is used. For a detailed description of these options, refer to the GCC's guide page, type the Man GCC on the command line. Debugging and parsing option GCC supports several commissioning and profiling options In these options you will most often use the -g and -pg option. -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 other C compilers No characteristics, you can use -g and -o (produce optimized code) in GCC. This is very useful because you can debug your code as possible with the final product as possible. Use this at the same time When two options you must know, some code you have written is already optimized by GCC. For more information on debugging C procedures, please see the next section "Use GDB Debug C Programs". -Pg Option Tell GCC to add additional code in your program, execute, generate gprof, analyze the analysis information to display The time consumption of your program. For more information on GPROF, please refer to the "GPROF" section. With the GDB debug GCC program Linux contains a GNU debugger called GDB. GDB is a power to debug C and C programs Debugger. 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 the variables in your program. It makes you set a breakpoint To stop the program on the specified code line. It allows you to perform your code in a row. 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 well to distributes; 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 a lot of options on the command line. You can run the following way GDB: GDB When you run GDB in this way, you can specify the program you want to debug. This will tell GDB to load an executable file named FNAME. You can also use GDB to check an abnormal termination of the program Core file, or connected to 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 compile for debugging in order Make GDBs working properly, you must make your program contain debugging information when compiling. The debugging information contains the type of each variable in your program and the address mapping in the executable file and the line number of the source code. GDB uses this information The source code is associated with the machine code. Open the debug option with the -g option in compile. GDB basic command GDB supports a lot of commands to enable you to achieve different features. These commands are loaded from simple files to allow you to check. The complex command of the stack content, Table 27.1 lists some of the commands you use when using GDB debugging. Want to know more about GDB, please refer to the GDB guide page. Table 27.1. Basic GDB command. Command Description File loading The executable that wants to debug. Kill terminates the program being debugging. List lists a part of the source code generating the execution file. Next executes a row source code but does not enter the function inside. STEP executes a row source code and enters the function inside. RUN execution The currently debugging program quit terminates GDB Watch allows you to monitor a variable value regardless of whether it is changed. Break sets breakpoints in the code, which will cause the program to be hanged here. Make makes you Exiting GDB can re-generate executable. Shell allows you to execute a UNIX shell command without leaving GDB. GDB supports a lot of command editing features like UNIX shell programs. You can press Tab keys like it in Bash or TCSH to let GDB Help you fill a unique command, if you don't have the only GDB list all matching commands. You can also flip history commands with cursor keys. GDB application example this section To teach you step by step, a GDB debugger. The program being debugged is quite simple, but it shows the typical application of GDB. The program will be debugged. This program is called Greeting, it shows a simple Greetings, then list it again. #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 } Compile it with the following command: gcc -ggdb -o test test.c This program is executed as follows: The string is hello there is the first line of the Strinted Backward IS output is correct, but the second line is printed Things are not what we expect. The output we want should be: The String Printed Backward Is Ereht Olleh is not working properly due to some reasons. Let us use GDB to see where the problem is, type the following Command: GDB GRETING ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------- Note: Remember to open the debug option when compiling the Greeting program. --- -------------------------------------------------- --------------------------- If you forget to pass the program to debug as a parameter, you can be in GDB The prompt uses the file command to load it: (GDB) File Greeting This command will load the Greeting executable is like you are loaded in the GDB command line. At this point you can run Greeting with GDB's Run command. It is. When it is running in GDB, the result is about like this: (GDB) Run Starting Program: / root / greeting the string is hello the the stringprinted backward is program evted with code 041 This output is running outside GDB The result is the problem, why does not work in the reverse sequence? In order to find out the crux, we can set a breakpoint after the MY_PRINT2 function for the FOR statement, the specific practice is to type the list command three times in the GDB prompt, list the source Code: (GDB) List (GDB) List (GDB) list ------------------------------------ ---------------------------------------- ---- Tip: Press the GDB prompt to repeat the last command. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- 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 String2); 27} Depending on the source program listed, you can see where you want to break points on line 24, type the following command to set breakpoints under the GDB command line prompt: (GDB) Break 24 GDB will make The following response: BreakPoint 1 at 0x139: file greeting.c, line 24 (gdb) Now type the RUN command, will result in the following output: starting program: / root / greckpoint 1, my_print2 (String = 0xBffFDC4 "Hello there") at greeting.c: 24 24 24 string2 [size-i] = String [i] You can see how the error is generated by setting an observation point of the value of the string2 [size - i] variable. The practice is to type: (gdb) Watch string2 [size - i] GDB will make the following: WatchPoint 2: String2 [size - i] You can now use the next command to perform the for loop: (GDB) Next After a cycle, GDB tells us that string2 [size - i] is **** h ****. GDB tells you this information with the following display: WatchPoint 2, String2 [size - i] old value = 0 **** 00 'new value = 104 **** h' my_print2 (String = 0xBfffdc4 "Hello there") at greeting.c: 23 23 for (i = 0; I this value is expected. Later The result of several cycles is correct. When i = 10, the value of expression string2 [size - i] is equal to **** E ****, the value of Size - i is equal to 1, the last character has been copied It's in the new string. If you execute the loop again, you will see that there is no value assigned to string2 [0], and it is the first character of the new string, because the malloc function initializes them when allocating internal. A null character. So String2's first character is empty characters. This explains why there is no output when printing string2. Now find out the problem Where is it, correct this error. You have to change the shift of the first character written in String2 in the code to size - 1 instead of size. This is because String2 is 12, but from The start offset is 0, the characters in the string from the offset 0 to the offset 10, the offset amount 11 is empty character. In order to make the code normal work, there are many modifications. One is another than string The actual size of 1 variable. This is the code of this solution: #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 The String Printed Backward IS% S / N ", String2);} Additional C programming tool SLACKWARE Linux also includes some C development tools we have not mentioned. This section describes these tools and their typical usage. XXGDB XXGDB is a XXGDB-based graphical interface. XXGDB includes all features on the gdb of the command line. XXGDB allows you to perform a commonly used command by pressing the button. Place the breakpoint is also displayed You can use the following command in a xterm window to run it: xxgdb You can initialize XXGDB with any valid command line option in GDB. In addition, XXGDB also has some unique command line options, and these options are listed in Table 27.2 lists these options. Table 27.2. Xxgdb command line option. Option Description DB_Name Specifies the name of the debugger used, the default is GDB. DB_PROMPT Specifies the debugger prompt, default is GDB. GDBINIT specifies the file name of the initialization of the command file of the GDB, default. gdbinit. NX tells xxgdb does not execute .Gdbinit file. Bigicon uses a large icon. Calls You can use the following path in the SunSite.Unc.edu FTP site: /pub/linux/devel/lang/calls.tar.z to get Calls, some older version of the Linux CD-ROM release is also included. Because it is a useful tool, we also introduce here. If you feel useful, from BBS, FTP, or another CD-ROM Locate a copy. Calls calls the GCC's preprocessor to process the given source program file, then output the function of these files to call the tree map. --------------- -------------------------------------------------- ------------- Note: Install Calls on your system, perform the following steps after you log in as a superuser: 1. Unzip and Untar files. 2. CD Enter Calls Untar after establishing Subdirectory. 3. Move the file called Calls to the / usr / bin directory. 4. Move the file called calls.1 to the directory / usr / man / man1. 5. Delete / TMP / calls directory. These steps will install the Calls program and its guide page to your system. ---------------------------- -------------------------------------------------- - When the call is printed with the tracking result, it gives the file name of the file in the function behind the function: main [test.c] If the function is not to the file given to calls, Calls doesn't know Where is the function called, only the name of the function is displayed: Printf calls does not output the recursive and static function. The recursive function is displayed below: Fact <<< Recursive in factorial.c >>> Static function like this display: Total [Static in Calculate.c] As an example, assume that the following program is handled with a Calls: #include main () {char my_string [] = "hello there"; my_print (my_string); my_print2 (my_string);} my_print (char * {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 The stringprinted backward is% s / n ", string2);} Run the C code after running: #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