Guest User

Untitled

a guest
Feb 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. The compilation of a C++ program involves three steps:
  2.  
  3. Preprocessing: the preprocessor takes a C++ source code file and deals with the #includes, #defines and other preprocessor directives.
  4. The output of this step is a "pure" C++ file without pre-processor directives.
  5.  
  6. Compilation: the compiler takes the pre-processor's output and produces an object file from it.
  7.  
  8. Linking: the linker takes the object files produced by the compiler and produces either a library or an executable file.
  9.  
  10. Preprocessing
  11. The preprocessor handles the preprocessor directives, like #include and #define. It is agnostic of the syntax of C++, which is why it
  12. must be used with care.
  13.  
  14. It works on one C++ source file at a time by replacing #include directives with the content of the respective files (which is usually
  15. just declarations), doing replacement of macros (#define), and selecting different portions of text depending of #if, #ifdef and
  16. #ifndef directives.
  17.  
  18. The preprocessor works on a stream of preprocessing tokens. Macro substitution is defined as replacing tokens with other tokens
  19. (the operator ## enables merging two tokens when it makes sense).
  20.  
  21. After all this, the preprocessor produces a single output that is a stream of tokens resulting from the transformations
  22. described above. It also adds some special markers that tell the compiler where each line came from so that it can use those
  23. to produce sensible error messages.
  24.  
  25. Some errors can be produced at this stage with clever use of the #if and #error directives.
  26.  
  27. Compilation
  28. The compilation step is performed on each output of the preprocessor. The compiler parses the pure C++ source code (now without
  29. any preprocessor directives) and converts it into assembly code. Then invokes underlying back-end(assembler in toolchain) that
  30. assembles that code into machine code producing actual binary file in some format(ELF, COFF, a.out, ...).
  31. This object file contains the compiled code (in binary form) of the symbols defined in the input.
  32. Symbols in object files are referred to by name.
  33.  
  34. Object files can refer to symbols that are not defined. This is the case when you use a declaration, and don't provide a definition
  35. for it. The compiler doesn't mind this, and will happily produce the object file as long as the source code is well-formed.
  36.  
  37. Compilers usually let you stop compilation at this point. This is very useful because with it you can compile each source code
  38. file separately. The advantage this provides is that you don't need to recompile everything if you only change a single file.
  39.  
  40. The produced object files can be put in special archives called static libraries, for easier reusing later on.
  41.  
  42. It's at this stage that "regular" compiler errors, like syntax errors or failed overload resolution errors, are reported.
  43.  
  44. Linking
  45. The linker is what produces the final compilation output from the object files the compiler produced. This output can be either a
  46. shared (or dynamic) library (and while the name is similar, they haven't got much in common with static libraries mentioned earlier)
  47. or an executable.
  48.  
  49. It links all the object files by replacing the references to undefined symbols with the correct addresses. Each of these symbols
  50. can be defined in other object files or in libraries. If they are defined in libraries other than the standard library, you need
  51. to tell the linker about them.
  52.  
  53. At this stage the most common errors are missing definitions or duplicate definitions. The former means that either the definitions
  54. don't exist (i.e. they are not written), or that the object files or libraries where they reside were not given to the linker.
  55. The latter is obvious: the same symbol was defined in two different object files or libraries.
Add Comment
Please, Sign In to add comment