The release of Linux contains many software development tools, many of which are used for C and C ++ application development. This article introduces the features that can be used for C application development and debugging in Linux. tool the purpose of this article is to introduce how to use the C compiler in Linux
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. the purpose of this article is to introduce how to use the C compiler and other C programming tools in Linux, rather than the C programming tutorial.
Linux programming tool gnu c Compiler
Linux programming tool gnu c compiler (GCC) 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 for Linux programming tools
The basic usage of the GCC compiler. gcc command is usually followed by some options and file names:
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 the options you will most commonly use.
GCC options for Linux programming tools
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 group 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 compile test. in c, the profile information is created for the prof command and the debugging information is added 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) a program named. out executable file. for example, the following command will generate. out File: gcc test. c
You can use the-o compilation option to specify a file name for the generated executable file instead of. out. for example. the c program of C is compiled into an executable file named count. you will enter the following command: gcc-o count. c
The Linux Programming tool GCC also has the compilation option to specify how much the compiler processes. -c indicates that GCC only compiles the source code as the target code and skips the assembly and connection steps. this option is frequently used because it makes compilation of multiple C programs faster and easier to manage. the target code file created by GCC is missing. o extension.
The-S compilation option tells GCC to stop compiling after an assembly language file is generated for C code. the default extension of assembly language files generated by GCC is. s. -E indicates that the compiler only processes the input file. when this option is used, the pre-processor output is sent to the standard output instead of stored in the file.
Optimized items
When you use the Linux programming tool 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 optimize 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,-g and-pg options are most commonly used.
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 these two options at the same time, you must be aware that some of your code 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.