GCC compilation command GCC Optimization

Source: Internet
Author: User

Less Optimization-> more optimization:

O0 --> O1 --> O2 --> O3

-O0 indicates no optimization,-O1 indicates the default value, and-O3 indicates the highest optimization level.

English explanation:

'-O'

'-O1'

Optimize. Optimizing compilation takes somewhat more time, and

Lot more memory for a large function.

With '-O', the compiler tries to reduce code size and execution

Time, without specify Ming any optimizations that take a great deal

Of compilation time.

'-O' turns on the following optimization flags:

-Fdefer-pop

-Fdelayed-branch

-Fguess-branch-probability

-Fcprop-registers

-Floop-optimize

-Fif-conversion

Fif-conversion2

-Ftree-ccp

-Ftree-dce

-Ftree-dominator-opts

-Ftree-dse

-Ftree-ter

-Ftree-lrs

-Ftree-sra

-Ftree-copyrename

-Ftree-fre

-Ftree-ch

-Funit-at-a-time

-Fmerge-constants

'-O' also turns on'-fomit-frame-pointer 'on machines where doing

So does not interfere with debugging.

'-O' doesn' t turn on'-ftree-sra' for the Ada compiler. This

Option must be explicitly specified on the command line to be

Enabled for the Ada compiler.

'-O2'

Optimize even more. GCC performs nearly all supported

Optimizations that do not involve a space-speed tradeoff.

Compiler does not perform loop unrolling or function inlining when

You specify '-O2'. As compared to '-O', this option increases

Both compilation time and the performance of the generated code.

'-O2' turns on all optimization flags specified by '-O'. It also

Turns on the following optimization flags:

-Fthread-jumps

-Fcrossjumping

-Foptimize-sibling-CILS

-Fcse-follow-jumps-fcse-skip-blocks

-Fgcse-lm

-Fexpensive-optimizations

-Fstrength-reduce

-Frerun-cse-after-loop-frerun-loop-opt

-Fcaller-saves

-Fpeephole2

-Fschedule-insns-fschedule-insns2

-Fsched-interblock-fsched-spec

-Fregmove

-Fstrict-aliasing

-Fdelete-null-pointer-checks

-Freorder-blocks-freorder-functions

-Falign-functions-falign-jumps

-Falign-loops-falign-labels

-Ftree-vrp

-Ftree-pre

Please note the warning under '-fgcse 'about invoking'-O2 'on

Programs that use computed gotos.

'-O3'

Optimize yet more. '-O3' turns on all optimizations specified

'-O2' and also turns on the '-finline-functions ',

'-Funswitch-loops' and '-fgcse-after-reload' options.

'-O0'

Do not optimize. This is the default.

/// ============================= There is another OS option ======================== ================

Http://hi.baidu.com/ah__fu/blog/item/cc9fd19b 8bdc9eaf4b3.html

When studying the makefile compilation driver, we found that the GCC command line contains an-OS optimization option.

Check the GCC document and find-O0,-O1,-O2,-O3, that is, no-OS is found.

I finally found out that this article illustrates the role of-OS:

Http://www.linuxjournal.com/article/7269

The original-OS is equivalent to-O2.5. All-O2 optimization options are used, but the code size is not reduced.

The detailed description is as follows:

Level 2.5 (-OS)

The special optimization level (-OS or size) enables all-O2 optimizations that do not increase code size; it puts the emphasis on size over speed. this operation des all second-level optimizations, except t for the alignment optimizations. the alignment optimizations skip space to align functions, loops, jumps and labels to an address that is a multiple of a power of two, in an architecture-dependent manner. skipping to these boundaries can increase performance as well as the size of the resulting code and data spaces; therefore, these particle optimizations are disabled. the size optimization level is enabled as: gcc-OS-o test. c

In gcc 3.2.2, reorder-blocks is enabled at-OS, but in gcc 3.3.2 reorder-blocks is disabled.

====================================

Supplement: In the GCC official documentation, I found a description about-OS:

Http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimi ze-Options.html # Optimize-Options

// ================================================ ========

Http://blog.csdn.net/ison81/archive/2009/05/07/415 8576. aspxBacktrace and fomit-frame-pointer options

In fact, all levels of gcc optimization (-O,-O2,-O3, etc.) will enable -fomit-frame-pointerwww.wxsmam.com. The function of this option is not to save the frame pointer during function calls, it is fp on ARM, so we cannot trace the call stack according to the APCS conventions. However, you can still use the bt command in GDB to view the call stack. Why? After learning that GDB v6 is supported DWARF2, it means that it can not rely on fp to trace the call stack (see the http://gcc.gnu.org/ml/gcc/2003-10/msg00322.html ).

It seems that if you want to dynamically display the call stack without using GDB in the code, you can only turn off-fomit-frame-pointer during compilation.

// ============================= Gcc parameter reference: ======================================

[Introduction]

Gcc and g ++ are the gnu c & c ++ compiler gcc/g ++. A total of four steps are required during compilation.

1. pre-process and generate the. I file [pre-processor cpp]

2. Do not convert the pre-processed files into assembly languages to generate the file. s [compiler egcs]

3. a. o file is generated by means of compilation into the target code (machine code) [assembler as]

4. Connect the target code to generate an executable program [linker ld]

[Parameter Details]

-X language filename

Set the language used by the file to make the suffix invalid and valid for later versions. that is, according to the Conventions, the C language suffix is. c, and C ++'s suffix is. C or. cpp. If you have a personality, the suffix of your C code file is. pig Haha, you need to use this parameter. This parameter applies to all file names after it, unless it is used by the next parameter.

Can I use the following parameters?

'C', 'Objective-C', 'c-header', 'c ++ ', 'cpp-output', 'Explorer ', and 'author er-with-cpp '.

You can understand English.

Example usage:

Gcc-x c hello. pig

-X none filename

Turn off the previous option www.shibasui.com, that is, let gcc automatically identify the file type based on the file name suffix.

Example usage:

Gcc-x c hello. pig-x none hello2.c

-C

Only activate preprocessing, compilation, and assembly, that is, he only makes the program into an obj file.

Example usage:

Gcc-c hello. c

It will generate the. o obj file

-S

Only activating preprocessing and compilation means compiling a file into assembly code.

Example usage

Gcc-S hello. c

It will generate the. s assembly code. You can view it in a text editor.

-E

Only activate preprocessing. If this file is not generated, You need to redirect it to an output file.

Example usage:

Gcc-E hello. c> pianoapan.txt

Gcc-E hello. c | more

Let's take a look. A hello word should also process 800 lines of code.

-O

Set the target name. By default, the gcc compiled file is a. out, which is hard to hear. If you feel the same way with me, change it. Haha

Example usage

Gcc-o hello.exe hello. c (Oh, windows is used to it)

Gcc-o hello. asm-S hello. c

-Pipe

Using pipelines instead of temporary files during compilation may cause some problems when using non-gnu compilation tools.

Gcc-pipe-o hello.exe hello. c

-Ansi

Disable the incompatible features of ansi c in gnu c and activate the proprietary features of ansi c (including prohibiting some asm inline typeof keywords, and preprocessing macros such as UNIX and vax,

-Fno-asm

This option is part of the feature that implements the ansi option. It prohibits the use of asm, inline, and typeof as keywords.

-Fno-strict-prototype

It only works for g ++. With this option, g ++ does not explicitly describe the number and type of parameters for functions without parameters, instead of having no parameters.

Whether this parameter is used or not, gcc considers that there is no explicitly stated type for a function without parameters.

-Fthis-is-varialble

Is to align with the traditional c ++, you can use this when the general variable is used.

-Fcond-mismatch

The second and third parameter types of conditional expressions do not match. The expression value is of the void type.

-Funsigned-char

-Fno-signed-char

-Fsigned-char

-Fno-unsigned-char

These four parameters are used to set the char type and decide to set the char type to unsigned char (the first two parameters) or signed char (the last two parameters)

-Include file

Include a code. Simply put, a file can be set with another file. The function is equivalent to using # include <filename> in the code.

Example usage:

Gcc hello. c-include/root/pianopan. h

-Imacros file

Extends the macro of the file to the input file of gcc/g ++. The macro definition does not appear in the input file.

-Dmacro

Equivalent to # define macro in C

-Dmacro = defn

Equivalent to # define macro = defn in C

-Umacro

Equivalent to # undef macro in C

-Undef

Cancel definition of any non-standard macros

-Idir

When you use # include "file", gcc/g ++ first searches for the header file in the current directory. If not, he returns to the default header file directory. If-I is used to specify the directory

Go back to the directory you created, and then find it in the regular order.

For # include <file>, gcc/g ++ will go to the directory specified by-I and cannot find it. Then, it will go to the default header file directory of the system.

-I-

Is to cancel the function of the previous parameter, so it is generally used after-Idir

-Idirafter dir

An error occurred while searching in the-I directory.

-Iprefix prefix

-Iwithprefix dir

Generally, if-I Directory Search fails, it will be searched under prefix + dir

-Nostdinc

Make the compiler no longer look for the header file in the system's default header file directory. It is generally used in conjunction with-I to specify the position of the header file.

-Nostdin C ++

The rule is not to be searched in the standard channels specified by g ++, but still in other paths. This option is used in the create libg ++ library.

-C

During pre-processing, do not delete the annotation information, which is generally used with-E. Sometimes the analysis program is very convenient to use.

-M

Generate file association information. You can use gcc-M hello. c to test all the source code that the target file depends on.

-MM

It is the same as the one above, but it will ignore the dependency caused by # include <file>.

-MD

It is the same as-M, but the output will be imported into the. d file.

-MMD

It is the same as-MM, but the output will be imported into the. d file.

-Wa, option

This option is passed to the assembler; if there is a comma in the option, the option is divided into multiple options, and then passed to the assembler

-Wl. option

This option is passed to the connection program. If there is a comma in the option, the option is divided into multiple options and then passed to the connected program.

-Llibrary

Library Used during compilation

Example usage

Gcc-lcurses hello. c

Compile the program using the ncurses Library

-Ldir

Specify the path of the database to be searched during compilation. For example, you can use your own library to create a directory. Otherwise

The compiler will only find it in the standard library directory. This dir is the directory name.

-O0

-O1

-O2

-O3

The compiler has four levels of optimization options.-O0 indicates no optimization,-O1 indicates the default value, and-O3 indicates the highest optimization level.

-G

Only the compiler generates debugging information during compilation.

-Maid

This option claims debugging information in stabs format, but does not include gdb debugging information.

-Gstabs +

This option claims debugging information in stabs format and contains additional debugging information for gdb only.

-Ggdb

This option will generate debugging information that can be used by gdb as much as possible.

-Static

This option will disable the use of dynamic libraries. Therefore, compiled items are generally large and do not need any

The dynamic connection library can be run.

-Share

This option will try to use the dynamic library, so the generated file is small, but the system needs to use the dynamic library.

-Traditional

Try to make the compiler support the traditional C Language Features

[References]

-Linux/UNIX Advanced Programming

Edited by zhongke Hongqi Software Technology Co., Ltd. Published by Tsinghua University Press

-Gcc man page

[ChangeLog]

-2002-08-10

Ver 0.1 released the original document

-2002-08-11

Ver 0.11 modify document format

-2002-08-12

Server 0.12 adds parameters for static and dynamic libraries

-2002-08-16

Ver 0.16 adds four phases of gcc compilation commands

Run gcc/egcs

***********************

GCC is the gnu c and C ++ compilers. In fact, GCC can compile three languages: C, C ++, and Object C (an object-oriented extension of C ). The gcc command can be used to compile and connect the C and C ++ source programs at the same time.

If you have two or a few C source files, you can easily use GCC to compile, connect, and generate executable files. For example, if you have two source files main. c and factorial. c, compile a program to calculate factorial.

Code:

-----------------------

List factorial. c

-----------------------

Int factorial (int n)

{

If (n <= 1)

Return 1;

Else

Return factorial (n-1) * n;

}

-----------------------

Listing main. c

-----------------------

# Include <stdio. h>

# Include <unistd. h>

Int factorial (int n );

Int main (int argc, char ** argv)

{

Int n;

If (argc <2)

{

Printf ("Usage: % s n \ n", argv [0]);

Return-1;

}

Else

{

N = atoi (argv [1]);

Printf ("Factorial of % d is % d. \ n", n, factorial (n ));

}

Return 0;

}

-----------------------

The following command can be used to compile and generate executable files and execute programs:

$ Gcc-o factorial main. c factorial. c

$./Factorial 5

Factorial of 5 is 120.

GCC can be used to compile both C and C ++ programs. Generally, the C compiler uses the extension of the source file to determine whether it is a C program or a C ++ program. In Linux, the extension of the C source file is. c, and the extension of the C ++ source file is. C or. cpp. However, the gcc command can only compile the C ++ source file, but cannot automatically connect to the library used by the C ++ program. Therefore, the g ++ command is usually used to compile and connect the C ++ program. The program automatically calls gcc for compilation. Suppose we have the following C ++ source file (hello. C ):

# Include <iostream>

Void main (void)

{

Cout <"Hello, world! "<Endl;

}

You can call the g ++ command to compile, connect, and generate an executable file as follows:

$ G ++-o hello. C

$./Hello

Hello, world!

* ******************* Main options of gcc/egcs *********

Common gcc commands

Option description

-Ansi only supports the ANSI standard C syntax. This option will disable some features of gnu c,

For example, asm or typeof keywords.

-C only compiles and generates the target file.

-DMACRO defines the MACRO with the string "1.

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

-E only runs the C pre-compiler.

-G generates debugging information. The GNU Debugger can use this information.

-IDIRECTORY: specify an additional header file to search for the path DIRECTORY.

-LDIRECTORY: specify an additional function library to search for the path DIRECTORY.

-Search for the specified LIBRARY when connecting to lLIBRARY.

-Msung optimizes code for 486.

-O FILE: generate the specified output FILE. Used to generate executable files.

-O0 is not optimized.

-O or-O1 optimized code generation.

-O2 is further optimized.

-O3 is further optimized than-O2, including the inline function.

-Shared object generation. It is usually used to create a shared library.

-Static prohibit the use of shared connections.

-UMACRO undefines MACRO macros.

-W does not generate any warning information.

-Wall generates all warning information.

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.