当前位置:   article > 正文

[An Introduction to GCC 学习笔记] 14 优化问题3_the provided gcc is too old, switching to its g++

the provided gcc is too old, switching to its g++ instead

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)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/224442
推荐阅读
相关标签
  

闽ICP备14008679号