Transfer from: http://www.linuxfans.org/nuke/modules.php? Name = News & File = Article & SID = 720 2006-6-23
Foreword
Chapter 27, books from "Slackware Linux Unleashed" (Third Edition):
Programming IN C. If there is anything about this translation, please contact me:
Mailto: con@nese.net.
There is a lot of software development tools in Linux issues. Many of them are developed for C and C applications. This article describes tools that can be used for C applications and commissioning under Linux. The main purpose of this article is to introduce How to use C compiler 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 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, function prototypes Generator (Automatic Function Prototypers).
Note: The PRETTY Print Programs automatically helps you format the source code to generate a consistent indentation format.
C language programming
Original: rick mcmullin
Foreword
Chapter 27, books from "Slackware Linux Unleashed" (Third Edition):
Programming IN C. If there is anything about this translation, please contact me:
Mailto: con@nese.net.
There is a lot of software development tools in Linux issues. Many of them are developed for C and C applications. This article describes tools that can be used for C applications and commissioning under Linux. The main purpose of this article is to introduce How to use C compiler 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 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, function prototypes Generator (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 the UNIX operating system. It was first written by Dennis Ritchie, Bell Lab, for UNIX-assisted development. The language is written. From then, C will become the most widely used computer language in the world.
C can be found in the programming field:
It is a very common language. Almost you can think of at least one C compiler on the computer. And its syntax and function library are unified on different platforms, this Features are very attractive to developers. The program is quickly performed with C. c is a system language on all versions of UNIX. C has a great development in the past 20s. In the 1980s The final US National Standards Institute released a C language standard called ANSI C. This ensures the consistency of C in the future on different platforms. There is also a C-facing in the 1980s. The expansion of the object is called C . C will be described in another article "C Programming".
The C compiler available on Linux is a GNU C compiler, which is based on the programming license of the Free Software Foundation, so you can freely release it. You can find it on Linux's distribution discs.
GNU C compiler
The GNU C compiler (GCC) issued with 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. This The section will show you how to use the most common options for GCC and some GCC compilers. Use GCC
Typically followed by some options and file names using the GCC compiler.
The basic usage of the GCC command is as follows:
The GCC [options] [filenames] command line option specifies the action performed on the command line. The next section will narrate some options you will use.
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 of the GCC options include more than one character. So you must be The option specifies its respective hyphens, just like most Linux commands, you cannot follow a set of options after a single hyphen. For example, the following two commands are different:
GCC -P -G Test.c
GCC -PG Test.c The first command tells GCC compilation
Test.c
The PROF command establishes a profile information and adds the debug information into the executable file. The second command only tells GCC
The gprof command establishes a profiling information.
When you don't have any option to compile a program, GCC will establish (assume that compile success) one name
A.out's executable. For example, the following command will generate a call in the current directory
A.out's file:
GCC Test.c you can use
-o compilation options instead of specifying a file name to generate the executable
A.out. For example
COP program compiled as a name called
COUNT's executable, you will enter the following command:
GCC -O count country.c
Note: When you use
When the -o option,
-o must be followed by a file name.
The GCC also has the compilation option for specifying the compiler processing.
The -c option tells GCC to skip the assembly and connection only to compile the source code as a target code. This option is very frequently used because it makes it easier when compiling multiple C processes, and it is easier to manage. By default GCC established Target code file has one
Extension of .o.
The -s compilation option tells the GCC to stop compilation language files after the C code is generated. The default extension of the assembly language file generated by the GCC is the .s. -E option indicates that the compiler is only preprocessing the input file. When this When the 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 use GCC to compile C code, 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 passed. Optimization. Many options can be used to tell GCC to generate smaller and faster executables on the basis of consuming more compile time and sacrifice, which is the most typical in these options.
-O and
-O2 option.
The -o option tells GCC to optimize the source code. These optimizations will make the program execute faster in most cases. -O2 option tells GCC to generate as small and as fast as possible. -O2 option will make compilation The speed is 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 generate faster code. These options are very special, and it is best to only be completely understood by these options. What is the compiled code? The effect is used again. For a detailed description of these options, please refer to the GCC's Guide page, type the Man GCC on the command line.
Debug and parsing options
GCC supports several commissioning and parsing options. In these options you will use most often
-g and
-pg option.
-g option tells 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, you can make -g and -o in the GCC (produce optimized code ) Use. This is very useful because you can debug your code as possible with the final product as possible. When you use these two options simultaneously, you must know that some code you wrote is already optimized. GCC made changes. For more information on debugging C program, please see the "GDB Debug C Progress".-PG Option Tell GCC to add additional code in your program, execute, generate Gprof To display the time consumption of your program. For more information on GPROF, please refer to the "GPROF" section.
Debug GCC program with GDB
Linux includes 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 GDB provided Some features:
It allows you to monitor the value of the variable in your program. It allows you to set breakpoints to stop execution on the specified code line. It allows you to perform your code in a line. Type on the command line
GDB and press Enter key 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
After GDB, you can specify a lot of options on the command line. You can also run the following way
GDB:
GDB
GDB, you can specify the program you want to debug. This will tell
GDB is loaded into an executable file called FNAME. You can also use
GDB to check an Core file that is generated by the program, or connects with a running program. You can refer to
GDB guide page or type on the command line
GDB -H gets a simple list of instructions on these options.
Compilant code for debugging
because
GDB works normally, you must make your program contain debugging information when compiling. Debug information contains the type of each variable in your program and the line number of the address map in the executable file 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 order
GDB supports a lot of commands to enable you to achieve different features. These commands are loaded from simple files to the complex command that allows you to check the content called the stack content, Table 27.1 lists you are using
Some commands that will be used when GDB debugging. I want to know
Please refer to the details of GDB
GDB's guide page.
Table 27.1. Basic GDB Command.
The command describes the executable file that File is loaded. Kill terminates the program that is 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 of the function. Step execute a row source code And enter the function inside .Run executes the current debugged program Quit Termination GDBWATCH allows you to monitor the value of a variable, regardless of whether it is changed. Break sets breakpoints in the code, which will make the program to be executed here .make allows you to re-generate the executable file without exiting GDB. 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 the Tab key like it in Bash or TCSH to let GDB help you make up a unique command. If you don't have unique, GDB will list all match commands. You You can also flip history commands with cursor keys. GDB application example
This section teaches you step by step with an example
GDB debugging program. The debugged program is quite simple, but it shows it.
Typical application of GDB.
The program that will be debugged will be listed below. This program is called
Greeting, it 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% SN", String);
}
Void my_print2 (char * string)
{
Char * string2;
Int size, i;
SIZE = Strlen (String);
String2 = (char *) Malloc (SIZE 1);
For (i = 0; i String2 [size - i] = string [i]; String2 [SIZE 1] = `'; Printf ("THE STRING Printed Backward IS% SN", String2); } Compile it with the following command: GCC -O Test Test.c This program shows the following results: The string is hello there The first line of the String Printed Backward IS is correct, but the second line of printing is not what we expect. The output we want should be: The String Printed Backward Is Ereht Olleh Due to some reason, My_print2 functions are not working properly. Let us use GDB looks at where the problem is, type the following command: GDB GRETING Note: Remember in compilation The debug option opens when a Greeting program. If you forget the program to debug as a parameter to pass when entering the command GDB, you can The GDB prompt uses the file command to load it: (GDB) file greeting This command will be loaded Greeting executable is like you The GDB command line is loaded. At this time, you can run Greeting with GDB's Run command. When it is running in GDB, the result will be like this: (GDB) RUN Starting program: / root / greeting The string is hello there The String Printed Backward IS Program evted with code 041 This output is The result of running outside GDB. The problem is why don't you work in the reverse order? In order to find out the crux, we can MY_PRINT2 function After the FOR statement, set a breakpoint, the specific practice is GDB prompt type List commands three times, listing the source code: (GDB) List (GDB) List (GDB) List Tips: The GDB prompt pressing the car will repeat the previous command. First type The output of 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 it, GDB will be executed again The list command gives the following output: 11 my_print (char * string) 12 { 13 Printf ("THE STRING IS% SN", String); 14} 15 16 my_print2 (char * string) 17 { 18 char * string2; 19 Int size, i; 20, press another carriage return will be listed The remainder of the Greeting program: 21 size = strlen (String); 22 string2 = (char *) Malloc (SIZE 1); 23 for (i = 0; i 24 string2 [size - i] = String [i]; 25 string2 [size 1] = `'; 26 Printf ("THE STRINTED BACKWARD IS% SN", String2); 27} According to the source program listed, you can see where you want to break the point on, on the 24th line. The GDB command line prompt is typed to set the breakpoint below: (GDB) BREAK 24 GDB will make the following response: Breakpoint 1 AT 0x139: File Greeting.c, Line 24 (GDB) Type it now The Run command will produce the following output: Starting program: / root / greeting The string is hello there BreakPoint 1, My_Print2 (String = 0xBfffdc4 "Hello there") at Greeting.c: 24 24 string2 [size-i] = String [i] You can set an observation String2 [size - i] Value of the value of the value of the variable is what the error is generated, and the practice is to type: (gdb) Watch string2 [size - i] GDB will make the following response: WatchPoint 2: string2 [size - i] can now be used Next command to perform a step For loop: (GDB) NEXT After the first cycle, GDB tells us The value of 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 I = 10, expressions String2 [size - i] is equal to `E` The value of size - i is equal to 1, and the last character has been copied to the new string. If you do 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 initials them into empty (null) characters when allocating memory So the first character of String2 is empty characters. This explains why there is no output when printing string2. Now find out where the problem is, it is easy to fix this error. You have to write the code in string2 The first character of the first character is changed 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 10, offset 11 is empty character retained. In order to make the code work properly, there are many modifications. One is a variable that sets another actual size of the string. 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% SN", 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 String2 [Size2 - I] = String [i]; String2 [size] = `'; Printf ("THE STRING Printed Backward IS% SN", String2); } Another C programming tool Slackware Linux also includes some C development tools we have not mentioned. This section will introduce these tools and their typical usage. xxgdb XXGDB is A graphical interface based on the X Window system. XXGDB includes command lines All features on the GDB. XXGDB allows you to perform a common command by pressing the button. Place the breakpoint is also displayed. You can be in one Type the following command to run it in the XTERM window: Xxgdb you can use Any valid command line option in GDB is initialized XXGDB. In addition XXGDB also has some unique command line options, and these options are listed in Table 27.2. Table 27.2. Xxgdb command line option. Select DB_NAME to specify the name of the debugger used, the default is GDB.DB_PROMPT specifies the debugger prompt, default is gdb.gdbinit Initialization The file name of the command file of GDB is default. .gdbinit. NX tells xxgdb does not execute .Gdbinit file. Bigicon uses a large icon. Calls You can Sunsite.Unc.edu FTP Site with the following path: /Pub/linux/devel/lang/calls.tar.z To get Calls, some old version of the Linux CD-ROM release is also included. Because it is a useful tool, we also introduce here. If you think useful, from BBS, FTP, or another CD -ROM Lote. Calls Calls the GCC's preprocessor to process the given source program file, then output the function call the tree in these files. Note: Installing on your system Calls, perform the following steps as you log in as a super user: 1. Unzip and Untar files. 2. CD enters The subdirectory established after Calls Untar. 3. Tell the name Calls's file moves to / usr / bin directory. 4. Move the file called calls.1 to the directory / usr / man / man1. 5. Delete / TMP / Calls directory. These steps will The Calls program and its guide page are installed on your system. when When Calls prints the debugging result, it gives the file name of the file in the file behind the function behind the function: Main [Test.c] If the function is not In the documentation given in Calls, Calls doesn't know where to call 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 is like this: Total [Static In Calculate.c] as an example, assume Calls handles the following procedure: #include Main () { Char my_string [] = "Hello there"; MY_PRINT (my_string); MY_PRINT2 (my_string); } MY_PRINT (Char * String) { Printf ("THE STRING IS% SN", 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 String2 [Size2 - I] = String [i]; String2 [size] = `'; Printf ("THE STRING Printed Backward IS% SN", String2); } Will produce the following output: 1 main [test.c] 2 my_print [test.c] 3 Printf 4 my_print2 [test.c] 5 strlen 6 malloc 7 Printf Calls has a lot of command line options to set different output formats, please refer to more information about these options. Calls's guide page. Method is to type on the command line Calls -h. CPROTO CPROTO reads into the C source file and automatically generates a prototype declaration for each function. CPROTO can save you a lot of time to define a function prototype when you write a program. If you let CPROTO handles 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% SN", * 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 String2 [Size2 - I] = String [i]; string2 [size] = `'; Printf ("THE STRING Printed Backward IS% SN", String2); } You will get the following output: / * Test.c * / INT main (void); INT my_print (char * string); INT my_print2; this output can be redirected to a defined function prototype. Indent The Indent utility is another programming utility included in Linux. This tool simply produces a beautiful indentation format for your code. Indent also has many options to specify how to format your source code. For more information, please see InDent's Guide page, type on the command line Indent -h. The following example is the default output of Indent: Run the previous C code before: #include MAIN () { Char my_string [] = "Hello there"; MY_PRINT (my_string); MY_PRINT2 (my_string);} MY_PRINT (Char * String) { Printf ("THE STRING IS% SN", * 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 String2 [Size2 - I] = String [i]; String2 [size] = `'; Printf ("THE STRING Printed Backward IS% SN", String2); } C code after INDENT: #include Main () { Char my_string [] = "Hello there"; MY_PRINT (my_string); MY_PRINT2 (my_string); } MY_PRINT (Char * String) { Printf ("THE STRING IS% SN", * 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 String2 [Size2 - I] = String [i]; String2 [size] = `'; Printf ("THE STRING Printed Backward IS% SN", String2); } Indenter does not change the substance of the code, but only change the appearance of the code. Make it more readable, which is always a good thing. GPROF Gprof is installed in your Linux system A program in the / usr / bin directory. It allows you to analyze your programs to know which part of the program is time consuming. GPROF will tell you the number of times each function called and each function is percentage of time. If you want to improve your program performance, this information is very useful. In order to use Gprof on your program, you must use GPROF on your program, you must Plus -pg options when compiling. This will cause the program to generate a file called GMon.out when executed. Gprof generates information on this file. After you run your program and generate the gmon.out file, you can use the following command to obtain an analysis: GPROF Tip: The profiling data generated by GPROF is very large. If you want to check this data, it is best to redirect the output to a file. F2C and P2C F2C and P2C is two source code conversion programs. The F2C converts the FORTRAN code to C code, and the P2C converts the PASCAL code to C code. When you install GCC, both programs will be installed. If you have some code written by Fortran or Pascal, F2C and P2C are very useful to you with C. The C code generated by the two programs is generally compiled by GCC without modification. If the Fortran or PASCAL program to be converted is smaller, you can use the F2C or P2C without any options. If you want to convert the program, you can use a lot of files, you may have to use some command line options. Use F2C on a Fortran program, enter the following command: F2C my_fortranprog.f note: The F2C requires the extension of the program being converted to or a . To replace a PASCAL program as a C program, enter the following command: P2C my_pascalprogram.pas The file name of the C source code generated by these two programs is the same as the original file name, but the extension is made by .f or .pas changes .c.