How to Use GCC in Linux

Source: Internet
Author: User

 

Compile:

The first step is to pre-compile. The-E parameter allows GCC to stop the compilation process after preprocessing:

Gcc-E hello. c-o hello. I

Pre-processing macro definition inserted into hello. I

Step 2: Compile hello. I as the target code, which can be done by using the-c parameter:

Gcc-c hello. I-o hello. o

It can also be directly generated through the source file.

Gcc-c hello. c

Step 3: link the generated target file to an executable file:

Gcc hello. o-o hello

It can also be directly generated through the source file.

Gcc-o hello. c

Warning:

1. The-pedantic option generates a warning when the extended syntax is used.

2. Use-Wall to generate as many warnings as possible for GCC.

3.-Werror, which requires GCC to handle all warnings as errors

Library dependency:

1. Most functions in Linux are default:

Put the header file in the/usr/include/directory.

The library files are stored in the/usr/lib/directory.

2. GCC must find the required header file and library file in its own way during compilation.

-I option can be used to add a new directory to the GCC header file search path.

For example, if there is a header file required for compilation in the/home/xiaowp/include/directory, you can use the-I option to make GCC find them smoothly:

Gcc-o test. c-I/home/xiaowp/include

-L option: 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/xiaowp/lib/directory, you can use the following command to make GCC find it smoothly:

Gcc-o test. c-L/home/xiaowp/lib-lfoo

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.

-Static option, force static Link Library

If there is a link in the/home/xiaowp/lib/directory, the required library files libfoo. so and libfoo.

To enable GCC to only use the static link library when linking, run the following command:

Gcc-o test. c-L/home/xiaowp/lib-static-lfoo

Option-O can tell GCC to reduce the code length and execution time at the same time, and its effect is equivalent to-O1.

The types of optimizations that can be performed at this level depend on the target processor, but they generally include Thread Jump and Deferred Stack Pops.

Option-O2 tells GCC not only to complete all-O1-level optimization, but also to make some extra adjustments, such as processor command scheduling.

In addition to completing all-O2-level optimization, option-O3 also includes loop expansion and other optimizations related to the processor features.

Generally, the larger the number, the higher the optimization level, and the faster the program runs.

Many Linux programmers like to use the-O2 option because it achieves an ideal balance between the optimized length, Compilation Time, and code size.

Time./test

Although optimization can bring better execution performance to the program, you should avoid optimizing the Code in the following scenarios:

The higher the optimization level during program development, the longer the compilation time is spent. Therefore, it is best not to use optimization options during development. Only when the software release or development is complete, to optimize the final generated code.

When resources are limited, some optimization options increase the size of executable code. If the memory resources that the program can apply for at runtime are very tight (such as some real-time embedded devices ), do not optimize the code, because the negative effects may have very serious consequences.

Some code may be deleted or rewritten when the code is optimized during tracing debugging, or reorganized to achieve better performance, which makes tracing and debugging very difficult.

Acceleration:

Use pipelines instead of temporary files in compilation,

-Pipe accelerates Compilation

Gcc-pipe foo. c-o foo

Common GCC options

-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.

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.