zhengjin

博客园 首页 新随笔 联系 订阅 管理

C++ programs typically go through six phases: edit, preprocess, compile, link, load and execute.

CPP program development phases 

Preprocessing and compiling

In phase 2, the programmer gives the command to compile the program. In a C++ system, a preprocessor program executes automatically before the compiler's translation phase begins (so we call preprocessing phase 2 and compiling phase 3). The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually include other text files to be compiled and perform various text replacements. In phase 3, the compiler translates the C++ program into machine-language code (also referred to as object code).

Preprocessing occurs before a program is compiled. Some possible actions are inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives. All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;). Preprocessor directives are processed fully before compilation begins.

preprocessor directives的格式已经在上面框里有说明了。根据这个,可以知道source code中想以#include,#define,#if, #ifdef, #ifndef, #else, #elif, #endif这类语句都是preprocessor directives了。

Linking

Phase 4 is called linking. C++ programs typically contain references to functions and data defined elsewhere, such as in the standard libraries or in the private libraries of groups of programmers working on a particular project. The object code produced by the C++ compiler typically contains "holes" due to these missing parts. A linker links the object code with the code for the missing functions to produce an executable image (with no missing pieces). If the program compiles and links correctly, an executable image is produced.


Comments

Every program should begin with a comment that describes the purpose of the program, author, date and time.

关于对源代码作注解这点需要做个说明。不光各个method,各个变量,或者某些语句需要注释,作为一个整体整个源代码文件也要被注解。这就是上面框中讲的,在代码最上方写明程序的目的、作者、日期等等。其中目的和日期是很重要的。我就曾经看过自己很早以前写的代码,但是因为文件放得挺乱,虽然代码中也有很多注释,自己看着却是缺少一个整体的概念,想不起来到底自己这写的是什么东西。

posted on 2009-04-05 03:46  zhengjin  阅读(261)  评论(0编辑  收藏  举报