Introduction to GCC in LINUX

Source: Internet
Author: User
Article title: Introduction to GCC in LINUX. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Author: sss
  
Preface
  
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. 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.
  
The program written in C runs fast.
  
C is the system language of all versions of UNIX.
  
C has made 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. it is based on the programming license of the Free Software Foundation, so it 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 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
  
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. 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. -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 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.
  
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.