) GNU Compiler tool chain

Source: Internet
Author: User
GNU Compiler tool chain
Introduction

The GNU Compiler set (also known as GCC) is a set of compilers and utilities for building binary files by using the source code of advanced languages. GCC is not only a standard compiler on GNU/Linux, but also a standard compiler developed by embedded systems. This is because GCC supports a variety of target architectures. For example, what we use here will focus on host-based R & D (Build Software for the platform we compile), but if we use cross-compilation (build for a different goal ), GCC provides 40 different structural systems. These include x86, rs6000, arm, PowerPC, and many others. GCC can also be used on more than 40 different host systems (such as Linux, Solaris, and Windows ).
In addition to standard C, GCC also supports many other programming languages. We can use GCC to compile C ++, Ada, Java, objective-C, and FORTRAN.
Here we will take a look at the basic features of GCC and some advanced topics (including optimization ). We will also understand some tools useful for program construction in GCC.
Compilation Overview
The GNU Compiler involves several different steps in building a target. These steps can be divided into four steps: preprocessing, compilation, assembly and link.
Preprocessing, compilation, and linking are usually included in one step. However, to demonstrate GCC functions, we need to present them independently.
In the preprocessing step, GCC will use the contained header file (. h) to the source file (. c) pre-processing. in this step, some commands will be line explanations, such as # ifdef, # include, # define, etc. the processing result is an intermediate file. Generally, this file is not generated for external requests, but we will present it here for completeness. The source files that have been preprocessed can be compiled (. s) at the compilation step. The files that have been compiled will be converted to machine commands in the compilation phase and the results will be the target file (. O ). Finally, the machine code will be linked together to generate an executable binary file.
The compilation phases are shown in the following table:
Stage Input and Output GCC example
Preprocessing *. C *. I gcc-E test. C-O test. I
Compile *. I *. s gcc-s test. I-o test. s
Compile *. S *. O gcc-C test. S-O test. o
Link *. O * GCC test. O-o Test
These are just preliminary usage. At present, we will go deep into GCC to learn about its usage. First, let's take a look at the various modes that demonstrate GCC usage. Then we will discuss some of the most common options of GCC. This includes debugging options to enable various warnings and optimizations. Then we will learn about some GCC tools related to GCC.
GCC mode (compilation, compilation, and link)
The simplest example at the beginning is to compile a C source file into an executable file. In this example, the entire source code is included in a file, so we use the following GCC command:
$ GCC test. C-o Test
Here we compile the test. c file and generate an executable statement named test. If we only want to generate the target file of the source code, we can use the-C option as follows:
$ Gcc-C test. c
By default, the target file named test. O is generated. But here we want to generate the target file named newtest. O. We can use the following command:
$ Gcc-C test. C-o newtest. o
Most programs we will develop will contain more than one file. GCC can easily handle this situation in the command line:
$ Gcc-o image first. C second. c third. c
Here we compile three source files and link them to the executable file named image.
Some useful options
In most cases, we store our header files in a directory different from our source file directory. Assume that the original code is saved in a subdirectory named./src, And the header file is saved in the./INC directory under the same directory. We can tell GCC to use the header file we provide:
$ GCC test. C-I ../INC-o Test
We can use multiple-I options to specify multiple subdirectories:
$ GCC test. C-I ../INC-I http://www.cnblogs.com/inc2-o Test
Here we specify another subdirectory named inc2. For software settings, we can specify symbolic constants in the compilation line. For example, we can define the following symbolic constant in our header file or source code:
# Define test_configuration
We can easily use the-D option to define a similar symbolic constant in the command line:
$ Gcc-dtest_configuration test. C-o Test
The benefit of specifying a command line is that we do not need to modify the source file to modify its behavior.
The last useful option is the source and Assembly embellishment list. Consider the following command line:
$ Gcc-C-g-wa,-Ahl,-l test. c
The most interesting thing about this command line is the-wa option, which will pass the following options to the Assembly stage and then use the Assembly to embellish the c Source Path.
Compile warning
If an error is detected during compilation, GCC will exit the compilation process, and the warning information shows the potential problems we need to fix, although an executable file may still be generated. GCC provides a wide range of warning systems, but these options must be enabled to take advantage of the warning range that can be detected.
The most commonly used GCC tool to detect warning information is the-wall option. This will open all warning information of a specified type, which is composed of the most common problems in the program. The usage is as follows:
$ Gcc-wall test. C-o Test
The warning options opened in the-wall option are as follows:
Option usage
Unused-function Warning: a static function, but not defined
Unused-label labels that are declared but not used
Unused-parameter warning unused function parameters
Unused-variable warning declared local variables not used
Unused-value warning calculated but not used
Format verifies available parameters such as the format string in printf Based on the specified Format String
Implict-int warning when the declared type is not specified
Warning when implict-function is used before function declaration
Char-subscripts warning when the character is used as the marker of the array
Missing-braces warning when the initial set is not included
Parentheses: Warning If () is omitted, causing misunderstanding
The return-type warning function is declared as int by default or the return value is missing.
Sequence-point Code Warning when element is suspicious
The default value of switch is missing in the switch statement, which indicates that the switch parameter can be specified.
Strict-aliasing strict rules for variable Conversion
Unknown-pragmas warning # The Pragma command is not recognized
Uninitilized warning the variables used are not initialized (only available at-O2 optimization level)
-Wall is synonymous with-all-warnings. The table above lists the various warning options that are allowed in-wall.
In this case, we should note that most options have an opposite form, so we can disable them. For example, if we want to enable the-wall option but want to disable unused warning sets, we can use the following command:
$ Gcc-wall-wno-unused test. C-o Test
The following table lists some useful options that are not enabled in-wall.
Option usage
Cast-align warning when a pointer is converted and the assigned value is increased
Sign-compare warning when a signed number is compared with an unsigned number and an incorrect value is generated
Missing-prototypes warning when a global function is used without the function prototype Declaration
Packed warning if a structure provides packaging properties without packaging events
Padded warning if a structure is filled with a value
Unreachable-code: if the Code does not execute a warning
Inline warning when an inline label is marked but cannot be implemented
Disabled-optimization warning when the optimizer cannot execute the specified Optimization
The last useful warning option is-werror. This option indicates that when an error is detected, the compiler will treat the warning as an error and exit the compilation process. This option is quite useful for generating high-quality code.
Debugging options
If we want to use a symbolic link to the debugger to debug our code, we can specify the-G option to generate call information for GDB. The-G option specifies a parameter to specify the format to be generated. If we want to debug information using dwarf-2
Format to generate debugging information. We can use the following command:
$ Gcc-The gdwarf-2 test. C-o Test
Other tools:
Next, let's look at some GNU tools that are useful in our R & D process.
First, how do we know the size of the executable program or target file? The size utility lets us know the text size (number of command lines) and data and BSS segments. Example:
$ Size test. o
Text Data BSS dec hex filename
789 256 4 1049 419 test. o
The size of the target file test. O is listed here. We found that the text size (Directive and constant) is 789 bytes, the data segment is 256 bytes, And the BSS segment (automatically set to 0 at the beginning) is 4 bytes. If we want to know more details, we can use the objdump utility. We can use the-Syms parameter to know the symbol table of the executable program or target file, as shown in the following example:
$ Objdump-Syms test. o
This will generate a list of available symbols in the target file, including their types (text, BSS, data), length, and offset. We can also use the disassemble parameter to disassemble the program, as shown in the following example:
# Objdump? Disassemble test. o
This generates a list of functions in the target file and commands generated by GCC.
Finally, the NM program can be used to understand the symbols in the target file. This program will not only list detailed information about each symbol and symbol type. Some other options are available, so we can get more detailed content from the NM homepage.

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.