GCC compiler options and optimization tips

Source: Internet
Author: User
Article title: GCC compiler options and optimization tips. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

The default optimization level for most programs and libraries during compilation is "2" (with the gcc option: "-O2") and is compiled on the Intel/AMD platform by default based on the i386 processor.

If you only want the compiled program to run on a specific platform, you need to execute more advanced compiler optimization options to generate code that can only run on a specific platform.

One way is to modify the Makefile file in each source code package, find the CFLAGS and CXXFLAGS variables (C and C ++ compiler compilation options) and modify their values.
Some source code packages, such as binutils, gcc, and glibc, have Makefile FILES in each sub-folder, so it is too tired to modify them!

Another simple method is to set the CFLAGS and CXXFLAGS environment variables. Most configure scripts use these two environment variables to replace the values in Makefile.
However, a few configure scripts do not. they must be manually edited.

To set the CFLAGS and CXXFLAGS environment variables, you can execute the following command in bash (or write it into. bashrc to be the default value ):
Export CFLAGS = "-O3-march = "& CXXFLAGS = $ CFLAGS
This is the minimum setting to ensure that it works on almost all platforms.

The "-march" option indicates that binary code is compiled for a specific cpu type (it cannot be run on a lower-level cpu ),
Intel is usually: pentium2, pentium3, pentium3m, pentium4, pentium4m, pentium-m, prescott, nocona
Note: pentium3m/pentium4m is the mobile P3/P4 used in the notebook; pentium-m is the cpu of the client I/II notebook;
Prescott is P4 with SSE3 (famous for its ability to boil eggs); nocona is the latest P4 with EMT64 (64-bit) (it can also fry eggs)
AMD is usually: k6, K6-2, k6-3, athlon, athlon-tbird, athlon-xp, athlon-mp, opteron, athlon64, athlon-fx
AMD generally uses DIYer, so you don't have to explain it.

If you do not complain about "segmentation fault, core dumped" during compilation, the "-O" optimization parameter you set is generally no problem.
Otherwise, reduce the optimization level ("-O3"-> "-O2"-> "-O1"-> cancel ).
Personal opinion: The server can use "-O2", which is the safest optimization parameter (set); the desktop can use "-O3 ";
Too many custom optimization options are not encouraged. In fact, there is no obvious speed difference between them (sometimes "-O3" is slower ).

The compiler is very sensitive to hardware, especially when a high level of optimization is used. any memory error may cause a fatal failure.
Therefore, do not overclock your computer during compilation (I always drop the frequency when compiling key programs ).

Note: the order of options is very important. if two options conflict with each other, the next one prevails.
For example, "-O3" will enable the-finline-functions option, but you can use "-O3-fno-inline-functions" to disable both the-O3 function and the function embedding function.

It is declared that "-O2" has enabled the vast majority of security optimization options, so you don't have to worry about those options.
First, let's talk about the items added to "-O3" based on "-O2". you can add them as needed (relatively secure ):
Gcc-3.4.4
-Finline-functions allows the compiler to select some simple functions to expand in the called area.
-Fweb assigns a pseudo register to each web struct.
-Frename-registers tries to get rid of the false dependency in the code. this option is very effective for machines with a large number of registers.
Gcc-4.0.2
-Finline-functions description:
-Funswitch-loops: move the variable without changing the value in the loop body out of the loop body.
-Fgcse-after-reload ** does not quite understand its meaning ** [which Daxia knows how to explain it to younger brother? thank you first]

After "-O3" is completed, the "-OS" option commonly used in embedded systems is also very important, it means to optimize the size of the generated binary code, it opens all the "-O2" open options, therefore, we generally think that the potential awareness of low-efficiency execution of binary code generated by "-OS" is wrong! Of course, the difference between this option and "-O2" is that it prohibits all spaces inserted for alignment based on "-O2, that is, all options of the "-falign-*" series are disabled. Whether or not such disabling reduces the code execution efficiency depends on different programs. it is said that in some cases, "-OS" is 14% more efficient than "-O3! Please explore it yourself in practice...

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

Next I would like to give a brief introduction to the several items that I think are more important [gcc-3.4.4]
[Note] all the items listed here are non-default options. you only need to add the required options.

-W disable the output of warning messages

-Werror converts all warnings to errors

-Wall: displays all warning messages

-V: displays the current version number of the compiled program.

-V Specifies the version of gcc to run. It is valid only when multiple gcc versions are installed.

-Ansi compiles programs according to the ANSI standard, but does not limit GNU extensions that do not conflict with the standard (this option is generally not used)

-If you want to restrict the code to comply with ISO standards strictly, enable this option at the same time on the basis of "-ansi" (rarely used)

-Std = Specifies the C language standard (c89, c99, gnu89). This option disables the extended keywords asm, typeof, and inline of gnu c (this option is generally not used)

-The static connector ignores the dynamic connection library and resolves all references by directly including the static target file to the result target file.

-The shared connector generates the shared object code, which can be dynamically connected to the program at runtime to form a complete executable body.
If you use the gcc command to create a shared library as its output, this option prevents the connector from treating the missing main () method as an error.
To work properly, you should use the "-fpic" option and the compilation of the target platform option to form all the shared target modules of the same library.

-Shared-libgcc: This option specifies that libgcc of the shared version is used. this option is invalid on machines without the shared version libgcc.

-Specs = The gcc driver reads the file to determine which options should be passed to those child processes.
This option can overwrite the default configuration by specifying the configuration file. the specified file will be processed after the default configuration file is read to modify the default configuration.

-Pipe uses pipelines instead of temporary files to exchange output from one stage to another, which can speed up compilation. Recommended.

-O Specifies the output file, which is valid for all outputs. Because only one file can be specified, do not use this option when multiple output files are generated.

-- Help displays the list of gcc command line options. when used with "-v", the options accepted by the processes called by gcc are also displayed.

[1] [2] [3] Next page

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.