Common GCC compilation options

Source: Internet
Author: User
Tags add numbers

When using GCC to compile a program, the compilation process can be divided into four stages:
Pre-processing)
Compile (Compiling)
Assembly)
Link (linking)

If you have a hello. c file, run the following command:
GCC hello. C-O hello
./Hello

Staged Compilation:
Preprocessing: gcc-e hello. C-O hello. I
Compile: gcc-C hello. I-O hello. o
Link: GCC hello. O-O hello

Parameters used:
-Wall can enable GCC to generate as much warning information as possible. We recommend that you always use the-wall option when compiling source code with GCC. They not only help programmers write more robust programs, it is also a powerful tool for tracking and debugging programs.
Gcc-wall hello. C-O hello
-Werror: it requires GCC to treat all warnings as errors, which is useful when using automatic compilation tools (such as make.
Gcc-wall-werror hello. C-O hello
-On, N refers to an integer ranging from 0 to 2 or 3, indicating code optimization. The larger the number, the better the optimization. -O is equivalent to-O1.
Gcc-wall-o-werror hello. C-O hello

Library dependency:
It is rare to develop software in Linux without using third-party function libraries. Generally, one or more function libraries must be supported to complete the corresponding functions. From the programmer's perspective, the function library is actually a collection of header files (. h) and library files (. So or.. Although most functions in Linux place header files in the/usr/include/directory by default, and the library files in the/usr/lib/directory, but not all cases are like this. For this reason, GCC must have its own way to find the required header files and library files during compilation.

GCC uses the Directory Search method to find the required files. The-I option can add a new directory to the GCC header file search path. For example, if the header files required for compilation exist in the/home/test/include/directory, you can use the-I option to make GCC find them smoothly:

# GCC Foo. C-I/home/test/include-O foo

Similarly, if you use a library file that is not in the standard location, you can use the-L option to add a new directory to the GCC library file search path. For example, if there is a link to the library file libfoo. So in the/home/test/lib/directory, you can use the following command to make GCC find it smoothly:

# GCC Foo. C-L/home/test/lib-lfoo-O foo

It is worth explaining that the-L option instructs GCC to connect to the library file libfoo. So. The naming conventions for library files in Linux are as follows: it should start with three letters of Lib. Because all library files follow the same rules, therefore, when you use the-L option to specify the name of the Linked Library file, you can save lib with three letters. That is to say, when GCC processes-lfoo, it automatically links to libfoo. so file.

Library files in Linux are divided into two categories. so) and static link library (usually. end A), the difference between the two is only that the Code required for program execution is dynamically loaded at runtime, or static load at compilation. By default, GCC preferentially uses the dynamic link library when linking. Static Link Library is considered only when the dynamic link library does not exist. If necessary, you can add the-static option during compilation, use a static Link Library. For example, if there is a link in the/home/xiaowp/lib/directory, the library file libfoo is required. so and libfoo. a. To enable GCC to only use the static link library when linking, run the following command:

# GCC Foo. C-L/home/xiaowp/lib-static-lfoo-O foo

Debugging
A powerful debugger not only provides programmers with the means to track program execution, but also helps them find a solution to the problem. For Linux programmers, GDB (GNU Debugger) works with GCC to provide a complete debugging environment for Linux-based software development.

By default, GCC does not insert debugging symbols into the generated binary code during compilation, because this increases the size of executable files. If you need to generate debugging symbol information during compilation, you can use the-G or-ggdb option of GCC. When GCC generates debugging symbols, it also adopts a hierarchical approach, developers can add numbers 1, 2, or 3 after the-G option to specify the number of debugging information added to the Code. The default value is 2 (-G2). The generated debugging information includes extended symbol table, row number, local or external variable information. Level 3 (-G3) contains all debugging information in Level 2 and macros defined in source code. Level 1 (-G1) does not contain local variables and debugging information related to row numbers, so it can only be used for tracing and stack dumping. Tracing refers to the function call history during the running process of the monitoring program. Stack dumping is a method to save the execution environment of the program in the original hexadecimal format, both are commonly used debugging methods.

Debugging symbols generated by GCC are universally adaptive and can be used by many debuggers. However, if GDB is used, you can also use the-ggdb option to include the debugging information dedicated to GDB in the generated binary code. The advantage of this method is that it can facilitate debugging of GDB, but the disadvantage is that other debuggers (such as DBX) may not be able to perform normal debugging. Option-ggdb accepts the same debugging level as-G, which has the same impact on the output debugging symbols.

Note that any debugging option will increase the size of the final binary file and increase the overhead of the program during execution, therefore, debugging options are generally used only in the software development and debugging phase. The impact of debugging options on the size of generated code can be seen from the following comparison process:

# GCC optimize. C-o optimize
# Ls optimize-l
-Rwxrwxr-x 1 test 11649 Nov 20 optimize (debugging option not added)
# Gcc-G optimize. C-o optimize
# Ls optimize-l
-Rwxrwxr-x 1 test 15889 Nov 20 optimize (add debugging options)

Although debugging options increase the file size, many software in Linux still use debugging options for compilation in the test version or even the final release version, this is a distinctive feature of Linux.
Although GCC allows debugging Symbol Information to be added while optimizing, the optimized code will be a great challenge for debugging itself. After the code is optimized, the variables declared and used in the source program may not be used any more, and the control flow may suddenly jump to an unexpected place, loop statements may become everywhere because of loop expansion. All these are a nightmare for debugging. It is recommended that you do not use any optimization options during debugging. optimization is considered only when the program is finally released.

Acceleration

The process of converting source code into executable files involves many intermediate steps, including preprocessing, compilation, compilation, and connection. These processes are actually completed by different procedures. In most cases, GCC can complete all background work for Linux programmers and automatically call corresponding programs for processing.

One obvious drawback is that when GCC processes each source file, it eventually needs to generate several temporary files to complete the corresponding work, which leads to a slowdown in processing speed. For example, when GCC processes a source file, it may need a temporary file to save the output of preprocessing, a temporary file to save the output of the compiler, and a temporary file to save the output of the assembler, reading and writing these temporary files obviously takes some time. When software projects become very large, the cost may become very heavy.

The solution is to use a more efficient communication method-Pipeline provided by Linux. It can be used to connect two programs at the same time, and the output of one program will be directly used as the input of another program, so that temporary files can be avoided, however, compilation consumes more memory.

Pipeline used during compilation is determined by the GCC-pipe option. The following command uses the GCC Pipeline Function to increase the compilation speed:

# Gcc-pipe Foo. C-o foo

Pipeline is used to compile small projects. The difference in compilation time may not be obvious, but the difference will become very obvious in large projects with many source codes.

File Extension

When using gcc, you must be familiar with common extensions and understand their meanings. To help you learn how to use GCC, these extensions are listed as follows:

. C original program;
. C ++ original program;
. Cc c ++ original program;
. Cxx C ++ original program;
. M objective-C original program;
. I the original C program that has been preprocessed;
. II the original C ++ program that has been preprocessed;
Original program of. s combination language;
Original program of. s combination language;
. H pre-processing file (header file );
. O target file;
. A. archive file.

Common GCC options

As an important compiling environment for C/C ++ in Linux, GCC has powerful functions and many compilation options. To facilitate future compilation, We will list the common options and descriptions as follows:

-C: Notify GCC to cancel the link, that is, compile the source code and generate the target file at the end;
-Dmacro defines the specified macro so that it can be verified by # ifdef in the source code;
-E is delivered to the standard output without compiling the output of the Preprocessing Program;
-G3 gets detailed information about the debugging program, which cannot be used together with the-O option;
-Idirectory: add the specified directory at the start point of the file search path;
-The llibrary prompts that the linked program contains the specified library when creating the final executable file;
-O,-O2, and-O3 enable the optimization status. This option cannot be used together with the-G option;
-S requires the Compilation Program to generate the assembler output from the source code;
-V starts all alarms;
-Wall cancels the compilation operation when an alert is generated, which indicates that the alert is regarded as an error;
-Werror: cancel the compilation operation when an alarm occurs, that is, the alarm is treated as an error;
-W: Disable all alarms.

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.