[An Introduction to GCC 学习笔记] 14 优化问题3

Posted 漫小牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[An Introduction to GCC 学习笔记] 14 优化问题3相关的知识,希望对你有一定的参考价值。

Optimization and Debugging

  • With GCC it is possible to use optimization in combination with the debugging option ‘-g’. Many other compiler do not allow this.
  • When using debugging and optimization together, the internal rearrangements carried out by the optimized program in the debugger. For example, temporary variables are often eliminated, and the ordering of statements may be changed.
  • However, when a program crashes unexpectedly, any debugging information is better than none - so the use of ‘-g’ is recommended for optimized programs, both for development and deployment. The debugging option ‘-g’ is enabled by default for releases of GNU packages, together with the optimizaiton option ‘-O2’.

Optimization and Compiler Warnings

  • When optimization is turned on, GCC can produce addtional warnings that do not appear when compiling without optimization.
  • As part of the optimization process, the compiler examines the use of all variables and their initial values - this is referred to as data-flow analysis. It forms the basis for other optimization strategies, such as instruction scheduling. A side-effect of data-flow analysis is that the compiler can detect the use of unintialized variables.
  • While GCC will usually find most uninitialized variables, it does so using heuristics which will occasionally miss some complicated cases or falsely warning about others. In the latter situation, it is often possible to rewrite the relevant lines in a simpler way that removes the warning and improves the readability of the source code.

Compiling a C++ Program

The GNU C++ compiler provided by GCC is a true C++ compiler = it compiles C++ source code directly into a assembly language, which provides better support for error reporting, debugging and optimization.

  • The procedure for compiling a C++ program is the same as for a C program, but uses the command g++ instead of gcc.
  • The C++ fronted of GCC g++ uses many of the same options as the C compiler gcc. It also supports some additional options for controlling C++ language features.
  • Programs using C++ object files must always be linked with g++, in order to supply the appropriate C++ libraries. Attempting to link a C++ object file with the C compiler gcc will cause “undefined reference” errors for C++ standard library functions.

How the compiler Works

Compilation is a multi-stage process involving several tools, including the GNU Compiler itself. The complete set of tools used in the compilation process is referred to as a toolchain.
The sequence of commands executed by a single invocation of GCC consists of the following stages:

  • Preprocessing(to expand macros)
  • Compilation(from source code to assembly language)
  • Assembly(from assembly language to machine code)
  • Linking(to create the final executable)

以上是关于[An Introduction to GCC 学习笔记] 14 优化问题3的主要内容,如果未能解决你的问题,请参考以下文章

[An Introduction to GCC 学习笔记] 09 -Wall

[An Introduction to GCC 学习笔记] 14 优化问题3

[An Introduction to GCC 学习笔记] 14 优化问题3

[An Introduction to GCC 学习笔记] 13 优化问题2

[An Introduction to GCC 学习笔记] 10 Warn预编译

[An Introduction to GCC 学习笔记] 07 链接外部静态库