Embedded Development Learning Summary-gcc tools

Source: Internet
Author: User
Summary of embedded development-gcc tool-general Linux technology-Linux programming and kernel information. The following is a detailed description. [Directory]

1. Introduction to gcc

2. gcc Execution Process

3. Basic gcc usage and options

4. gcc error types and Countermeasures

5. Several related environment variables

1. Introduction to gcc
In Linux, gcc (gnu c Compiler) is a powerful and superior multi-platform Compiler launched by GNU. It is one of the representative works of GNU. Gcc is a super compiler that can compile executable programs on multiple hardware platforms. Its execution efficiency is 20% higher than the average efficiency of general compilers ~ 30%.

The gcc compiler can compile and connect the C and C ++ language source programs, programming sequences, and target programs into executable files. if the name of the executable file is not given, gcc will generate. out file. In Linux, the executable file does not have a uniform suffix. The system distinguishes the executable file from the unexecutable File Based on the file attributes. Gcc uses suffixes to differentiate the categories of input files. Next we will introduce some agreed rules of gcc.

. C is a suffix file, c language source code file;

. A is a file with a suffix. It is a file library consisting of the target file;

. C,. cc or. cxx is a C ++ source code file;

. H is a suffix file, which is the header file included by the program;

. I is a file with a suffix. It is a pre-processed C source code file;

. Ii is a file with a suffix. It is a pre-processed C ++ source code file;

. M is a suffix file, which is an Objective-C source code file;

The. o file is the compiled target file;

. S is a suffix file, which is an assembly language source code file;

. S is a precompiled assembly language source code file.

2. gcc Execution Process
Although we call gcc a c language compiler, the process of using gcc to generate executable files from C language source code files is not just a compilation process, instead, we need to go through four interrelated steps: Preprocessing (also called Preprocessing), Compilation, Assembly, and Linking ).

The command gcc first calls cpp for preprocessing. During the preprocessing process, it analyzes the file inclusion (include) and pre-compiled statements (such as macro definition define) in the source code file. Next, call the "cc0" command to compile the file. At this stage, the target file with the. o suffix is generated based on the input file. The assembly process is a step for the assembly language and calls as for work. Generally ,. S is the suffix of the assembly language source code file and assembly ,. the assembly language files suffixed with s are generated after pre-compilation and assembly. o is the target file with the suffix. After all the target files are generated, gcc calls ld to complete the final key work. This stage is the connection. During the connection phase, all target files are arranged in the proper location of the executable program. At the same time, the library functions called by this program are also connected to appropriate places from their respective archives.

3. Basic gcc usage and options
When using the gcc compiler, we must provide a series of necessary call parameters and file names. There are more than 100 gcc compiler call parameters, most of which may not be used at all. Here we only introduce the most basic and commonly used parameters.

The most basic usage of gcc is: gcc [options] [filenames]

Among them, options is the parameter required by the compiler, and filenames provides the relevant file name.

-C: only compiled, not connected to executable files, the compiler is only input. c and other source code file generation. o is a target file with a suffix. It is usually used to compile a subroutine file that does not contain the main program.

-O output_filename: Make sure the output file name is output_filename. The name cannot be the same as the source file name. If this option is not provided, gcc provides the preset Executable File a. out.

-G, generate the symbolic information necessary for the symbolic debugging tool (GNU gdb). To debug the source code, we must add this option.

-E only runs the C pre-compiler.

-O: optimizes compilation and connection of programs. With this option, the entire source code is optimized during compilation and connection. This improves the execution efficiency of executable files, however, the compilation and connection speed is correspondingly slower.

-O2: it is better to optimize compilation and connection than-O. Of course, the entire compilation and connection process will be slower.

-Idirname: add the directory specified by dirname to the program header file directory list, which is a parameter used during the pre-compilation process. The header file in the C program contains two situations:

A) # include

B) # include "myinc. h"

Class A uses angle brackets (<>) and Class B uses double quotation marks (""). For Class A, the pre-processing program cpp searches for corresponding files in the system preset include file directory (such as/usr/include). for Class B, cpp searches for header files in the current directory, the purpose of this option is to tell cpp that if the desired file is not found in the current directory, it will be searched in the specified dirname directory. In programming, if we need to distribute the included files in different directories, we need to use the-I option to provide the search path one by one.

-L and-L

The-l parameter is used to specify the library to be linked by the program, and the-l parameter is followed by the database name. What is the relationship between the database name and the real library file name? Taking the Math Library as an example, his library name is m and his library file name is libm. so. It is easy to see that the library name is removed from the header lib and tail. so of the library file name.

Now we know how to get the database name. When we need to use the library name libtest provided by a third party. so, we just need to put libtest. so copy to/usr/lib, add the-ltest parameter during compilation, and we can use libtest. so Library (of course libtest. so library function, we also need to work with libtest. so header files)

The libraries in/lib and/usr/local/lib can be directly linked using the-l parameter. However, if the library files are not stored in these three directories, instead, it is stored in other directories. If we only use the-l parameter, the link will still go wrong. The error message is probably "/usr/bin/ld: cannot find-lxxx ", that is, the link program ld cannot find libxxx in the three directories. so, then another parameter-L will be used. For example, the common X11 library is located in the/usr/X11R6/lib directory, we need to use the-L/usr/X11R6/lib-lX11 parameter during compilation. The-L parameter is followed by the Directory Name of the library file. For example, if we put libtest. so in the/aaa/bbb/ccc directory, the Link parameter is-L/aaa/bbb/ccc-ltest.

In addition, most libxxxx. so is just a link. Take RH9 as an example, such as libm. so it links to/lib/libm. so. x,/lib/libm. so.6 link to/lib/libm-2.3.2.so,

If there is no such link, an error will still occur, because ld will only find libxxxx. so, if you want to use the xxxx library, instead of libxxxx. so. x or libxxxx-x.x.x.so, just make a link.
Ln-s libxxxx-x.x.x.so libxxxx. so

It is always troublesome to manually write the link parameters. Fortunately, many library sdks provide programs that generate the link parameters. The name is usually xxxx-config, which is usually stored in the/usr/bin directory, for example, if the program for generating the Link parameter of gtk1.2 is gtk-config, run gtk-config? The following output is displayed in libs:-L/usr/lib-L/usr/X11R6/lib-lgtk-lgdk-rdynamic.

-Lgmodule-lglib-ldl-lXi-lXext-lX11-lm ", which is the gtk Link parameter required to compile a gtk1.2 program? Is there another parameter besides the libs parameter? Cflags is used to generate a header file containing the directory, that is, the-I parameter, which will be discussed below. You can try gtk-config? Libs? Cflags: Check the output result.

Now the question is how to use these output results. The most stupid way is to copy, paste, or copy the results. The clever way is to add this 'xxxx-config in the compilation command line? Libs? Cflags '. For example, compile a gtk program: gcc gtktest. c 'gtk-config? Libs? Cflags. Note that 'is not a single quotation mark, but the key on the left of the 1 key.

In addition to xxx-config, the new development kit usually uses pkg-config to generate link parameters. The usage is similar to xxx-config, But xxx-config is for a specific development kit, however, pkg-config contains the generation of many development kit link parameters? The list-all command can list all supported development kits. The usage of pkg-config is pkg-config pagName? Libs? Cflags, where pagName is the package name and pkg-config? List-all to list one of the list. For example, the name of gtk1.2 is gtk + and pkg-config gtk +? Libs? The role of cflags and gtk-config? Libs? Cflags is the same. For example:
Gcc gtktest. c 'pkg-config gtk +? Libs? Cflags'


-Include and-I

-Include is used to include header files. Generally, the header files contained in the source code are implemented using # include xxxxxx, and the-include parameter is rarely used. -The I parameter is used to specify the header file directory. Generally, the/usr/include directory does not need to be specified. gcc knows where to find it, however, if the header file is not in/usr/include, We need to specify it with the-I parameter. For example, if the header file is placed in the/myinclude directory, the-I/myinclude parameter must be added to the compile command line, if this parameter is not added, you will get a "xxxx. h: No such file or directory "error. -The relative path can be used for the I parameter. For example, if the header file is in the current directory, you can use-I. to specify it. What we mentioned above? The cflags parameter is used to generate the-I parameter.

-DMACRO defines the MACRO with the string "1.

-DMACRO = DEFN: Define the MACRO with the string "DEFN.

-UMACRO undefines MACRO macros.

-W does not generate any warning information.

-Wall displays all warning information, which can be considered as Warn and all. gcc compilation options have many options that can be combined, such as-lm (l option and libm. so)

-Static prohibit the use of shared connections.

-Shared libraries are used to compile shared libraries, such as gcc-shared test. c-o libtest. so.


4. gcc error types and Countermeasures
If the gcc compiler finds an error in the source program, it cannot continue or generate the final executable file. To facilitate modification, gcc provides error information. We must analyze and process these error information one by one, and modify the corresponding language to ensure the correct compilation and connection of source code. The error information provided by gcc can be divided into four categories. The following describes the causes and countermeasures.

Class 1: C syntax error
Error message: line n of the file source. c contains a syntax error (syntex errror ). This type of error is generally a C language syntax error. You should carefully check the nth line in the source code file and the program before this line. Sometimes you need to check the header file contained in this file. In some cases, a very simple syntax error occurs. gcc will give a lot of errors. The most important thing is to keep a clear mind and not be intimidated by it, if necessary, refer to the basic C language teaching materials.

Category 2: header file Error
Error message: the header file head. h (Can not find include file head. h) cannot be found ). This type of error occurs when the source code file contains a header file, which may be caused by incorrect header file name or directory name of the specified header file, or double quotation marks and angle brackets.

Category 3: Archive errors
Error message: the connection program cannot find the required function library, for example:

Ld:-lm: No such file or directory

This type of error occurs in the function library connected to the target file. The possible cause is that the function library name is incorrect, and the Directory Name of the specified function library is incorrect, the check method is to use the find command to find the corresponding function library name in a possible directory, determine the name of the file library and directory, and modify the name in the program and compilation options.

Category 4: undefined symbols
Error message: Undefined symbol ). This type of error occurs during the connection process. There may be two reasons: first, the source code file of the function or global variable defined by the user is not compiled or connected, or there is no definition yet. This requires the user to modify the source program based on the actual situation and give the definition body of the global variable or function. Second, the undefined symbol is a standard library function, the library function is used in the source program, but the name of the corresponding library is not given during the connection, or the Directory Name of the library is incorrect, in this case, you need to use the archive maintenance command ar to check which function library the library function is in. After confirming, modify the-l and-L items in the gcc connection options.

Excluding errors during compilation and connection, it should be said that this is only the simplest and most basic step in program design. Errors in this process are only the errors that we encounter when using C language to describe an algorithm. They are easier to eliminate. Let's write a program until the compilation and connection are passed. It should be said that at the beginning, the problems encountered during the program running are problems in algorithm design, A more mysterious point is that we do not have enough understanding of the problem and need to perform further testing, debugging, and modification. A program, a slightly complex program, often needs to be compiled, connected, tested, and modified multiple times. The program maintenance, debugging tools, and version maintenance we will learn below are used in the program debugging and testing process to solve the problems encountered during the commissioning phase.

5. Several related environment variables
PKG_CONFIG_PATH: Specifies the path of the pc file used by pkg-config. The default path is/usr/lib/pkgconf.
Ig: The pc file is a text file, and the extension is. pc. It defines the installation path, Libs parameters, and Cflags parameters of the Development Kit.
CC: used to specify the c Compiler
CXX: used to specify the cxx Compiler
LIBS: Keep up with the above? Libs functions almost
CFLAGS: Keep up with the above? Cflags functions almost
CC, CXX, LIBS, and CFLAGS are not commonly used for manual compilation and are sometimes used for configure.
Skip
Environment variable setting method: export ENV_NAME = xxxxxxxxxxxxxxxxx

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.