compiler optimization, about Cflags and cxxflags

Source: Internet
Author: User
Tags hash
compiler optimization, about Cflags and cxxflags
This article from Jin Bu "gcc compilation Optimization Guide" http://lamp.linux.gov.cn/Linux/optimize_guide.html excerpt can be personally understandable and recommend the use of the part, stay for reference.
One, Cflags and Cxxflags
Cflags represents the option for the C compiler, cxxflags represents the options for the C + + compiler. These two variables actually cover the two steps of compiling and compiling. The default optimization level for most programs and libraries at compile time is "2" (using the "-o2" option) and compiled with debug symbols, i.e. cflags= "-o2-g", cxxflags= $CFLAGS. In fact, "-O2" has enabled most of the security optimization options. On the other hand, since most of the options can be used at the same time for both variables, only the options that are only used for one of these variables are described at the end. [Reminder] The options listed below are not default options, and you can just add them as needed.

-o3 "-o1 ...-o3 optimization level"
-finline-functions
Allows the compiler to select some simple functions to expand in their use, a more secure option, especially when CPU two cache is large

-falign-functions=n
-falign-jumps=n
-falign-loops=n
-falign-labels=n
These four alignment options are opened in "-o2", where different default values are used depending on the platform N. If you want to specify n that is different from the default value, you can also specify it separately. For example, for l2-cache>=1m CPUs, specifying-FALIGN-FUNCTIONS=64 may achieve better performance. It is recommended that the value here be not explicitly specified when-march is specified.
Global options:

-pipe
You can speed up compilation by using pipelines rather than temporary files between different stages of the compilation process. Recommended use. "seems to work only at compile time," he said.
-fstack-check
To prevent the stack overflow from doing the necessary testing, you may need it only when running in a multithreaded environment.
Hardware architecture-related options [for x86 and x86_64 only]:

-march=cpu-type
Compiles binary code for a specific Cpu-type (cannot run on a lower-level CPU). Intel can be used by: Pentium2, Pentium3 (=pentium3m), Pentium4 (=pentium4m), Pentium-m, Prescott, Nocona, Core2 (new GCC-4.3). AMD can be used: k6-2 (=k6-3), Athlon (=athlon-tbird), Athlon-xp (=ATHLON-MP), K8 (=OPTERON=ATHLON64=ATHLON-FX)
-mfpmath=sse
P3 and ATHLON-XP levels and above CPUs support "SSE" scalar floating-point directives. This option is only recommended for processors above the P4 and K8 levels.
-mmmx
-msse
-msse2
-msse3
-m3dnow
-MSSSE3 (New GCC-4.3)
-msse4.1 (New GCC-4.3)
-msse4.2 (New GCC-4.3)
-msse4 (including 4.1 and 4.2,gcc-4.3 added)
Do you want to use the appropriate extended instruction set and built-in functions to select your own CPU? "Please refer to the flags in Cat/proc/cpuinfo: The information Inside"
Options passed to the assembler:

-wa,-march=cpu
Optimized according to specific CPUs: PENTIUMIII, Pentium4, Prescott, Nocona, Core, Core2; Athlon, Sledgehammer, Opteron, K8. "Puzzled, and the top of the-march there is no difference." 】
Options available only for cxxflags:

-fvisibility-inlines-hidden
By default, all inline functions are hidden, reducing the size of the exported symbol table, reducing the size of the file and improving performance, and we strongly recommend that you use this option when compiling any shared libraries. See the-fvisibility=hidden option.

March specifies the current CPU architecture, and mtune is the option that really works on a particular model of the CPU.

"A relatively secure policy cflags="-march=native-mtune=native instruction set parameter-pipe-o3 "; cxxflags parameter, let him equal clags directly. cxxflags= "${cflags}" "

Second, Ldflags

Is the option to pass to the connector. This is a variable that is often overlooked, and in fact it has an obvious effect on optimization.

-s deletes all symbol tables and all relocation information in the executable program. The result is the same as that of the Run command strip, which is more secure.
-WL,--Hash-style=gnu
Use the GNU-style symbolic hash format. Its dynamic link performance is significantly higher than the traditional SYSV style (default), but it generates executable programs and libraries that are incompatible with the old glibc and the dynamic linker.

Finally, the selected compilation parameters are written to a file in an environment variable.


Go from: http://xiayongchun2009.blog.163.com/blog/static/4908511320122610156565/

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.