C language programming in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. The Linux release contains many software development tools. Many of them are used for C and C ++ application development. This article describes the tools that can be used for C application development and debugging in Linux. This document describes how to use the C compiler and other C programming tools in Linux, rather than programming in C. In this article, you will learn the following:
· What is C
· Gnu c Compiler
· Use gdb to debug GCC applications
You can also see other useful C programming tools released with Linux. These tools include the source program beautification program (pretty print programs), additional debugging tools, and automatic function prototypers ).
Note: The source program beautification program (pretty print programs) automatically helps you format the source code to generate consistent indent formats.
What is C?
C is a common programming language that was widely used in the early days of UNIX operating systems. It was first written by Dennis Ritchie of Bell Labs for UNIX-assisted development. At the beginning, UNIX was written in assembly language and a language called B. Since then, C has become the world's most widely used computer language.
The reasons why C is so widely supported in programming are as follows:
· It is a very common language. Almost any computer you can think of has at least one C compiler that can be used. In addition, its syntax and function libraries are unified on different platforms. This feature is very attractive to developers.
· Programs written in C run fast.
· C is the system language of all versions of UNIX.
C has experienced great development in the past two decades. At the end of 1980s, the American National Standards Institute released a C language standard called ansi c. This ensures the consistency of C on different platforms in the future. In 1980s, a C-oriented extension called C ++ emerged. C ++ will be described in another article "C ++ programming.
The available C compiler on Linux is the gnu c compiler, which is based on the programming license of the Free Software Foundation and can be freely released. You can find it on the Linux release CD.
Gnu c Compiler
The gnu c compiler (GCC) released with Slackware Linux is a fully functional ansi c compatible compiler. If you are familiar with a C compiler on other operating systems or hardware platforms, you will be able to quickly master GCC. This section describes how to use GCC and some of the most common options for GCC compilers.
Use GCC
The GCC compiler is usually followed by some options and file names. The basic usage of the gcc command is as follows:
Gcc [options] [filenames]
The operation specified by the command line option will be executed on each given file on the command line. The next section describes some of the options you will use most frequently.
GCC options
GCC has more than 100 compilation options available. Many of these options may never be used, but some of the main options will be frequently used. Many GCC options include more than one character. Therefore, you must specify the respective characters for each option, and, just like most Linux commands, you cannot separate the characters with 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 to create profile information for the prof command during test. c compilation and add the debugging information to the executable file. The second command only tells GCC to create profiling information for the gprof command.
When you do not need any options to compile a program, GCC will create (assuming the compilation is successful) an executable file named a. out. For example, the following command will generate a file named 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 file instead of a. out. For example, compile a c program named count. C into an executable file named count. You will enter the following command:
Gcc-o count. c
Note: When you use the-o option,-o must be followed by a file name.
GCC also has the compilation option to specify how much the compiler processes. -The c option tells GCC to skip the Assembly and connection steps only by compiling the source code as the target code. This option is frequently used because it makes compilation of multiple C Programs faster and easier to manage. There is a. o extension in the target code file created by GCC.
The-S compilation option tells GCC to stop compilation after an assembly language file is generated for C code. The default extension of assembly language files generated by GCC is. s. The-E Option indicates that the compiler only preprocess the input file. When this option is used, the pre-processor output is sent to the standard output instead of stored in the file.
Optimization Options
When you use GCC to compile C code, it will try to complete the compilation at least and make the compiled code easy to debug. Easy debugging means that the compiled code has the same execution order 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 executable files at the expense of more Compilation Time and easier debugging. The most typical options are-O and-O2.
The-O option tells GCC to perform basic optimization on the source code. In most cases, these optimizations make program execution faster. -The O2 option tells GCC to generate code as small as possible and as fast as possible. The-O2 option will make compilation faster than-O. However, code execution is usually faster.
In addition to the-O and-O2 optimization options, there are also some low-level options for generating faster code. These options are very special, and it is best to use them only when you fully understand the effects these options will have on the compiled code. For a detailed description of these options, refer to the GCC guide page and type man gcc on the command line.
Debugging and profiling options
GCC supports several debugging and profiling options. Among these options, you will most often use the-g and-pg options.
The-g option tells GCC to generate debugging information that can be used by the GNU Debugger to debug your program. GCC provides a feature that is not available in many other C compilers. in GCC, you can combine-g with-O (to produce optimization code. This is useful because you can debug your code as close as possible to the final product. When you use both of these options, you must be aware that some of the code you have written has been modified by GCC during optimization. For more information about debugging C Programs, see the next section "Debug C Programs with gdb ".
-The pg option tells GCC to add additional code to your program. During execution, gprof profiling information is generated to show the time consumption of your program. For more information about gprof, see "gprof.
Debug the GCC program with gdb
Linux contains a GNU debugging program called gdb. Gdb is a powerful debugger used to debug C and C ++ programs. It enables you to observe the internal structure and memory usage of the program while the program is running. The following are some functions provided by gdb:
· It enables you to monitor the value of variables in your program.
· It enables you to set breakpoints so that the program stops running on the specified code line.
· It enables you to execute your code in one line.
Type gdb on the command line and press the Enter key to run gdb. If everything works properly, 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)
After you start gdb, you can specify many options on the command line. You can also run gdb in the following ways:
Gdb
When you run gdb in this way, you can directly specify the program to be debugged. This tells gdb to load the executable file named fname. You can also use gdb to check a core file generated due to program termination exceptions, or connect it to a running program. You can refer to the gdb guide or Type gdb-h on the command line to get a simple list of instructions on these options.
Compile the Code for Debugging (Compiling Code for Debugging)
To make gdb work normally, you must make your program contain debugging information during compilation. The debugging information includes the type of each variable in your program, the address ing in the executable file, and the source code line number. Gdb uses this information to associate the source code with the machine code.
Enable the debugging option with the-g option during compilation.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.