Acompiler is a program that translates acomputer program written in onecomputer language (thesource code) into an equivalent program written in the computer's nativemachine language. This process of translation, that includes several distinct steps is calledcompilation. Since the compiler is a program, itself written in a computer language, the situation may seem a paradox akin to thechicken and egg dilemma. A compiler may not be created with the resulting compilable language but with a previous available language or even in machine code.
Thecompilation output of a compiler is the result from translating orcompiling a program. The most important part of the output is saved to a file called anobject file, it consists of the transformation of source files into object files.
Note: |
The instructions of thiscompiled program can then be run (executed) by the computer if the object file is in an executable format. However, there are additional steps that are required for a compilation: preprocessing and linking.
Defines the time and operations performed by a compiler (i.e.,compile-time operations) during a build (creation) of a program (executable or not). Most of the uses of "static" in the C++ language are directly related to compile-time information.
The operations performed at compile time usually includelexical analysis,syntax analysis, various kinds ofsemantic analysis (e.g.,type checks, some of thetype casts, andinstantiation of template) andcode generation.
The definition of a programming language will specify compile time requirements that source code must meet to be successfully compiled.
Compile time occurs beforelink time (when the output of one or more compiled files are joined together) and runtime (when a program is executed). In some programming languages it may be necessary for some compilation and linking to occur at runtime.
Run-time, orexecution time, starts at the moment the program starts to execute and end as it exits. At this stage the compiler is irrelevant and has no control. This is the most important location in regards to optimizations (a program will only compile once but run many times) and debugging (tracing and interaction will only be possible at this stage). But it is also in run-time that some of thetype casting may occur and thatRun-Time Type Information (RTTI) has relevance. The concept of runtime will be mentioned again when relevant.
This is alternatively known as scanning ortokenisation. It happens before syntax analysis and converts the code intotokens, which are the parts of the code that the program will actually use. The source code as expressed as characters (arranged on lines) into a sequence of special tokens for each reserved keyword, and tokens for data types and identifiers and values. The lexical analyzer is the part of the compiler which removes whitespace and other non compilable characters from the source code. It uses whitespace to separate different tokens, and ignores the whitespace.
To give a simple illustration of the process:
intmain(){std::cout<<"hello world"<<std::endl;return0;}
Depending on the lexical rules used it might betokenized as:
1 = string "int"2 = string "main"3 = opening parenthesis4 = closing parenthesis5 = opening brace6 = string "std"7 = namespace operator8 = string "cout"9 = << operator10 = string ""hello world""11 = string "endl"12 = semicolon13 = string "return"14 = number 015 = closing brace
And so for this program the lexical analyzer might send something like:
1 2 3 4 5 6 7 8 9 10 9 6 7 11 12 13 14 12 15
To the syntactical analyzer, which is talked about next, to be parsed. It is easier for the syntactical analyzer to apply the rules of the language when it can work with numerical values and can distinguish between language syntax (such as the semicolon) and everything else, and knows what data type each thing has.
This step (also called sometimes syntax checking) ensures that the code is valid and will sequence into an executable program. The syntactical analyzer applies rules to the code, checking to make sure that each opening brace has a corresponding closing brace, and that each declaration has a type, and that the type exists, and that.... syntax analysis is more complicated than lexical analysis =).
As an example:
intmain(){std::cout<<"hello world"<<std::endl;return0;}
namespace.namespace "std", and see that it was a template.namespace existed again, then check to see if "endl" was in thenamespace.return, and then expect an integer value as the next token because main returns an integer, and it would find 0, which is an integer.This is a simple view of syntax analysis, and real syntax analyzers do not really work this way, but the idea is the same.
Here are some keywords which the syntax analyzer will look for to make sure you are not using any of these as identifier names, or to know what type you are defining your variables as or what function you are using which is included in the C++ language.
There are several factors that dictate how fast a compilation proceeds, like:
Experience tells that most likely if you are suffering from slow compile times, the program you are trying to compile is poorly designed, take the time to structure your own code to minimize re-compilation after changes. Large projects will always compile slower. Use pre-compiled headers and external header guards. We will discuss ways to reduce compile time in theOptimization Section of this book.
When you select your compiler you must take in consideration your system OS, your personal preferences and the documentation that you can get on using it.
In case you do not have, want or need a compiler installed on you machine, you can use a WEB free compiler available athttp://ideone.com (orhttp://codepad.org but you will have to change the code not to require interactive input). You can always get one locally if you need it.
There are many compilers and even more IDEs available, some are free and open source. IDEs will often include in the installation the required compiler (being GCC the most common).
One of most mature and compatible C++ compiler is on GCC, also known as the GNU Compiler Collection. It is a free set of compilers developed by the Free Software Foundation, withRichard Stallman as one of the main architects.
There are many different pre-compiled GCC binaries on the Internet; some popular choices are listed below (with detailed steps for installation). You can easily find information on the GCC website on how to do it under another OS.
Note: |

Integrated development environment is a software development system, that often includes an editor, compiler and debugger in an integrated package that is distributed together. Some IDEs will require the user to make the integration of the components themselves, and others will refer as the IDE to the set of separated tools they use for programming.
A good IDE is one that permits the programmer to use it to abstract and accelerate some of the more common tasks and at the same time provide some help in reading and managing the code. Except for the compiler the C++ Standard has no control over the different implementations. Most IDEs are visually oriented, especially the new ones, they will offer graphical debuggers and other visual aids, but some people will still prefer the visual simplicity offered by potent text editors likeVim orEmacs.
When selecting an IDE, remember that you are also investing time to become proficient in its use. Completeness, stability and portability across OSs will be important.
For Microsoft Windows, you have also the Microsoft Visual Studio Community (latest version 2019), currently freely available and includes most features. It includes a C++ compiler that can be used from the command line or the supplied IDE.
In the bookAppendix B:External References you will find references to other freely available compilers and IDEs you can use.
Cygwin:
MinGW + DevCpp-IDE
DJGPP:
set PATH=C:\DJGPP\BIN;%PATH%set DJGPP=C:\DJGPP\DJGPP.ENVshell=c:\dos\command.com c:\dos /e:2048 /pfiles=40fcbs=40,0Note: The GNU C++ compiler under DJGPP is namedgpp.
Xcode (IDE for Apple's OSX and iOS) above v4.1 uses Clang[2], a free and open source alternative to the GCC compiler and largely compatible with it (taking even the same command line arguments). The IDE also has an older version of the GCC C++ compiler bundled. It can be invoked from the Terminal in the same way as Linux, but can also be compiled in one of XCode's projects.
Note: |
To do: |