Linux distributions include a lot of software development tools. Many of them are used for C and C + + application development. This article describes the following can be used in the Linux C application development and debugging tools. The main thrust of this article describes how to use the Linux C compiler and other C programming tools, rather than the C programming language tutorial. In this article you will learn the following knowledge:
What is C
· GNU C compiler
* GCC using gdb to debug the application
You can also see that with the release of other useful Linux C programming tools. These tools include the source code beautification program (pretty print programs), additional debugging tools, automatic generation of prototype tools (automatic function prototypers).
Note: The source beautification program (pretty print programs) to help you format the source code automatically generated consistent indentation.
What is C?
C is a kind of early in the UNIX operating system widely used general-purpose programming language. It was first used by Dennis Ritchie of Bell Labs to develop and write the supporting UNIX, and UNIX is the beginning of assembly language and a language called B prepared. Since then, C has become the world's most widely used computer language.
C programming field in such a widely supported by the following reasons:
• It is a very common language. You can think of almost any kind of computer has at least one can use the C compiler. And its syntax and function libraries in different platforms are integrated, this feature is very attractive to developers.
* Use C to write very fast program execution.
· C are all versions of UNIX on the system language.
C in the past two decades have greatly developed. In the late 80s American National Standards Institute (American National Standards Institute) has released a program called the C ANSI C language standard. This further ensures the future of C on different platforms consistency. In the 80's also the emergence of a object-oriented extension of C called C + +. C + + will be in another article "C + + programming" in the description.
Available on the Linux C compiler is GNU C compiler, which established the Free Software Foundation's programming based on the license, it can be freely distributed. Linux distribution you can find it on CD.
GNU C compiler
With the Slackware Linux distribution of GNU C compiler (GCC) is a full-featured ANSI C compliant compiler. If you are familiar with other operating system or hardware platform, a C compiler, you will be able to quickly grasp the GCC. This section describes how to use the GCC compiler, GCC and some of the most common options.
Use GCC
Usually followed by a number of options and file name to use the GCC compiler. gcc command of the basic usage is as follows:
gcc [options] [filenames]
Command-line options specified on the command line operation performed on a file for each paper. The next section will describe some of you will most commonly used options.
GCC Options
Over 100 GCC compiler options are available. Many of these options you may never use, but some of the major options will be used frequently. Many of the GCC options include more than one character. So you must specify each option with their own characters, and like most of the Linux command as you can in a single hyphen followed by a set of options. For example, the following two commands are different:
gcc-p-g test.c
gcc-pg test.c
The first command tells GCC compile test.c order to establish when prof analysis (profile) information and debug information to the executable file. The second command only for the gprof command tells GCC to establish the information analysis.
When you do not have any option to compile a program, GCC will create (assuming compile successfully) an executable file named a.out. For example, the following command in the current directory, create a file called a.out:
gcc test.c
You can use-o compiler option to generate the executable file to specify a file name instead of a.out. For example, a program called count.c the C compiler for the name of the executable file count, you will enter the following command:
gcc-o count count.c
Note: When you use the-o option,-o must be followed by a file name.
GCC compiler also deal with a designated number of build options. -C option tells GCC compile only the source code for the object code and skip the compilation and link steps. This option is used very frequently because it makes the build more than C programs faster and easier to manage. By default, GCC to establish the object code file has a. O extension.
-S compiler option tells GCC C code generated for the assembly language file to stop compilation. GCC assembly language generated by the default file extension is. S. -E option instructs the compiler to preprocess the input file only. When this option is used, the pre-processor output is sent to standard output, rather than stored in the file.
Optimization Options
When you compile C code with GCC, it will try to compile with the least time to complete and so easy to debug compiled code. Means easy to debug compiled code and source code for the implementation of the same order, the compiled code is not optimized. There are many options can be used to tell GCC compile time and at the expense of consuming more easily generated based on the debugging of smaller, faster executable. The most typical of these options is-O and-O2 option.
-O option tells GCC on the basic optimization of the source code. In most cases, these optimization will make the program run faster. -O2 option tells GCC to generate as small and as fast as possible code. -O2 option will compile faster than when using-O slow. However, the implementation of the generated code is usually faster.
In addition to-O and-O2 optimization option, there are some low-level options for generating faster code. These options are very special, and preferably only when you fully understand these options will be on the compiled code of what the effects will go use. Detailed description of these options, please refer to GCC's Guide page, type the command line man gcc.
Debugging and analysis options
GCC supports several types of debugging and analysis options. In most of these options that you will use the-g and-pg option.
-G option tells GCC GNU debugger can be used generate debugging information for debugging your program. GCC provides a lot of other C compiler, there are no features, where you can make in the GCC-g and-O (generate optimized code) combined. This is useful because you can close with the final product as the case of debugging your code. You use both of these options you must be clear when you write some code has been optimized when GCC was changed. Debugging C program on more information see the next section, "Using gdb to debug C programs."
-Pg option tells GCC to join in your program where the extra code, implementation, analysis of information generated using gprof to display your program's time-consuming situation. For more information on gprof see "gprof" section.
GCC program debugging with gdb
Linux includes a call the GNU gdb debugger. gdb is used to debug C and C + + program's powerful debugger. It allows you to observe procedures in the program to run the internal structure and memory usage. Here are some of the features provided by gdb:
• It enables you to monitor your process variables.
• It allows you to set breakpoints to make the program on a specified line of code to stop execution.
• It enables you to his party line of your code.
Type in the command line and press the Enter key gdb can run gdb, if everything is normal, then, gdb will be activated and you will see on the screen like this:
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 in the following way gdb:
gdb
When you run this way gdb, you can directly specify the procedure you want to debug. This will tell gdb into the executable file named fname. You can also use gdb to check an abnormal termination due process generated core file or running programs with a linked. You can refer to the gdb Guide page or on the command line, type gdb-h to get a description of these options, a simple list.
To debug compiled code (Compiling Code for Debugging)
In order for gdb to work, you must make your application contains debugging information at compile time. Debugging information is contained in your application for each variable in the executable file type and the address mapping and the source code line number. gdb use the information to the associated source code and machine code.
At compile-time option with the-g debugging option open.
Basic commands gdb
gdb supports many of the commands so you can achieve different functions. These commands from simple file loading to allow you to check the contents of the call stack complex command, Table 27.1 lists the gdb debugging you will use with some commands. To learn more use gdb gdb see the guide page.
Table 27.1. Basic gdb commands.
Command description file
Load the executable file you want to debug. kill
Termination is to debug procedures. list
Implementation of the line of source code but does not enter inside the function. next
Implementation of the line of source code but does not enter inside the function. step
Implementation of the line of source code and enter the function inside. run
Implementation of the current program being debugged to quit
Termination gdbwatch
So you can monitor the value of a variable to be changed regardless of when. break
Set breakpoints in the code, which will here when the program execution is suspended. make
So that you can not quit gdb can re-create the executable file. shell
So that you can not leave on the implementation of the UNIX shell command gdb.
gdb supports many UNIX shell program with the same command editing features. You can like as in bash or tcsh in press Tab key to gdb to help you append a unique command, if not unique, then gdb will list all the matching commands. You can also use the cursor keys up and down changes the history command.
gdb Application examples
An example in this section teach you step by step with the gdb debugger. The program being debugged quite simple, but it shows the typical application gdb.
The following lists the program being debugged. This process is known as greeting, it displays a simple greeting, and then listed it in reverse order.
# 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
", 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] = `0 ';
printf ("The string printed backward is% s
", String2);
)
Compile it with the following command:
gcc-o test test.c
The program execution shows the following results:
The string is hello there
The string printed backward is
The first line of output is correct, but the second line of print is not what we expected. We envisaged the output should be:
The string printed backward is ereht olleh
For some reason, my_print2 function does not work properly. Let us use gdb to see where exactly is the problem, first type the following command:
gdb greeting
Note: remember to compile the debug option when greeting program to open.
If you forgot to enter an order to debug the program as an argument to gdb, gdb prompt, you can use the file command to load it:
(Gdb) file greeting
This command will be included in greeting as the executable file in the gdb command line where you load it the same.
Then you can use gdb's run command to run the greeting was. When it was running in the gdb in the approximate results like this:
(Gdb) run
Starting program: / root / greeting
The string is hello there
The string printed backward is
Program exited with code 041
The output and the same as running outside gdb. The question is, why reverse order printing did not work? Order to find out the crux, we can my_print2 function for setting a breakpoint after statement, specific practices are in the gdb command prompt, type the list three times, listed in the source code:
(Gdb) list
(Gdb) list
(Gdb) list
Tips: In the gdb prompt, press Enter to repeat a command master.
The first type the list command output 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 Qiangzai perform a list command, gives the following output:
11 my_print (char * string)
12 (
13 printf ("The string is% s
", String);
14)
15
16 my_print2 (char * string)
17 (
18 char * string2;
19 int size, i;
20
Then a carriage return will set out the procedures for the remainder of the greeting:
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] = `0 ';
26 printf ("The string printed backward is% s
", String2);
27)
According to the source list, you can see where to set breakpoint at line 24, in the gdb command prompt, type the following command to set a breakpoint:
(Gdb) break 24
gdb will respond to the following:
Breakpoint 1 at 0x139: file greeting.c, line 24
(Gdb)
Now and then type 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 through setting an observation string2 [size - i] value of the variable out of the observation point of view how is wrong, and practice type:
(Gdb) watch string2 [size - i]
gdb will respond to the following:
Watchpoint 2: string2 [size - i]
Now step by step with the next command to the implementation for the loop:
(Gdb) next
After the first cycle, gdb tells us string2 [size - i] value is `h`. gdb with the following display to tell you this information:
Watchpoint 2, string2 [size - i]
Old value = 0 `000 '
New value = 104 `h '
my_print2 (string = 0xbfffdc4 "hello there") at greeting.c: 23
23 for (i = 0; i
This value is expected. Several cycles later the results are correct. When i = 10, the expression string2 [size - i] value is equal to `e`, size - i value equal to 1, the last character has a new string by copying it in the.
If you then loop down, you will see no value has been assigned to string2 [0], while it is the first new character string, because the malloc function in the allocation of memory is initialized to empty them (null) characters. Therefore, the first character string2 is null. This explains why there is no output when printing a string2.
Now where to find the problem, fix the error is very easy. You have to write the code in the first character of string2 offset to size - 1 rather than size. This is because the size of string2 is 12, but the starting offset is 0, the character string from the offset 0 to offset 10, offset 11, reserved for the null character.
In order to make the code work properly there are many modification. One is a separate smaller than the actual size of a string variable. This is the code for 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
", 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] = `0 ';
printf ("The string printed backward is% s
", String2);
)
Another C programming tools
Slackware Linux distributions also include some we have not yet mentioned the C development tools. This section describes these tools and their typical usage.
xxgdb
xxgdb is a gdb X Window System based graphical interface. xxgdb includes the command line version of gdb all the features. xxgdb so you can pass the buttons to perform frequently used commands. Set the breakpoint where also used to display graphics.
You can be a Xterm window, type the following command to run it:
xxgdb
You can use gdb in any valid command line options to initialize xxgdb. In addition xxgdb some specific command line options, table 27.2 lists these options.
Table 27.2. Xxgdb command line options. Options
Description db_name
Specifies the name used by the debugger, the default is gdb. db_prompt
Specify the debugger prompt, the default is gdb. gdbinit
Gdb command initialization file specifies the file name, by default. Gdbinit. nx
Tell xxgdb not execute. Gdbinit file. bigicon
Use large icons.
calls
Sunsite.unc.edu FTP site, you can use the following path:
/ Pub / Linux / devel / lang / c / calls.tar.Z
To get calls, some older versions of Linux CD-ROM also comes with inside release. Because it is a useful tool, we also introduce here. If you find it useful, then, from the BBS, FTP, or another CD-ROM, get a copy. GCC calls call the pre-processor to handle the given source file, then export these files in the function call tree.
Note: to install on your system calls, log in as root user perform the following steps: 1. Unzip and untar files. 2. cd into the directory created after the calls untar. 3. Calls the name of the file to / usr / bin directory. 4. The name of the file to a directory calls.1 / usr/man/man1. 5. Delete / tmp / calls directory. These steps will guide program and it calls page containing your system installation.
When calls print out call tracking results, it is the function given in parentheses behind the function with the name of the file where:
main [test.c]
If the function is not given to the documents in the calls, calls not know from where the function is called, only the display name of the function:
printf
not recursive function calls and static output. Recursive function shown as the following look:
fact <<>>
Static function like this show:
total [static in calculate.c]
As an example, assume that calls dealing with the following procedures:
# Include
main ()
(
char my_string [] = "hello there";
my_print (my_string);
my_print2 (my_string);
)
my_print (char * string)
(
printf ("The string is% s
", 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] = `0 ';
printf ("The string printed backward is% s
", 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 a lot of command-line option to set different output formats, for more information about these options, please refer to calls guide page. Is the command line, type calls-h.
cproto
cproto read C source file and automatically generate prototypes for each function stated. With cproto can write programs for you to save a lot of the time used to define the function prototype.
If you let cproto handling 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
", * 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] = `0 ';
printf ("The string printed backward is% s
", String2);
)
You will get the following output:
/ * Test.c backup / bin / conf / data / log / maint / svn / tmp /
int main (void);
int my_print (char * string);
int my_print2 (char * string);
This output can be redirected to a custom function prototype include file.
indent
indent utility is included with Linux in other programming utility. This tool is simply to generate code for your beautiful indented format. indent, there are many options to specify how to format your source code. For more information on these options see the indent of the guide page, type the command line indent-h.
The following example is the default output indent:
Run indent the 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
", * 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] = `0 ';
printf ("The string printed backward is% s
", String2);
)
After running indent 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
", * 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] = `0 ';
printf ("The string printed backward is% s
", String2);
)
indent the code does not change the substance, but only to change the appearance of the code. Make it more readable, it is always a good thing.
gprof
gprof is installed on your Linux system's / usr / bin directory of a program. It enables you to analyze your program to know which part of the program in the implementation of the most time-consuming.
gprof program will tell you where each function is called the number and percentage of each function is executed the percentage of time. If you would like to improve the performance of your application if the information very useful.
In order to use your program gprof, you must compile the program with-pg option. This will program each execution produces a file called gmon.out. gprof analysis of information generated with this document.
You run your programs and documents produced gmon.out you can use the following command was analyzed information:
gprof
Program_name is to generate parameter gmon.out file name of the procedure.
Skills: gprof analysis of data generated by large, if you want to check the data, the best to redirect the output to a file.
f2c and p2c
f2c and p2c are two source code conversion. f2c to convert FORTRAN code to C code, p2c Pascal code to convert the C code. When you install the GCC will be installed both programs up.
If you have some written with FORTRAN or Pascal code rewritten to use C, then, f2c and p2c is useful to you. These two procedures generally do not produce the C code can be modified directly GCC compiler.
If you want to convert FORTRAN or Pascal program is relatively small, then use f2c or p2c can not add any options. If you want to change the procedure is large, contains many files, then you may use some command line options.
In a FORTRAN program on the use of f2c, enter the following command:
f2c my_fortranprog.f
Note: f2c conversion process required by the extension. F or a. F.
Make a Pascal program for the C program installed, enter the following command:
p2c my_pascalprogram.pas
The two procedures generated C source code file name are the same as the original file name, but the extension from. F or. Pas into. C.