Compiler is a combination of many programs. So which program takes care of which part during compilation? I read somewhere that the preprocessor program takes care of the C preprocessor directives(macros). Is that correct?
-
1While it's true that a C compiler used to be compromised of a few smaller programs, that's rarely the case today. Due to how [translation phases](https://en.cppreference.com/w/c/language/translation_phases) are specified it's usually a single program. If so there are usually options to stop after preprocessing though. – Some programmer dude Aug 29 '18 at 09:01
-
1_"..The C++ source file is processed by the compiler __as if__ the following phases take place, in this exact order:.."_ https://en.cppreference.com/w/cpp/language/translation_phases __As if__ is the key phrase here most compilers (these days) are just a single executable . – Richard Critten Aug 29 '18 at 09:02
-
It's also common for a compiler to have options to run the preprocessor only, or at least to save the preprocessor output. If that's what you're looking for consult the documentaiton for your compiler. – john Aug 29 '18 at 09:05
-
Oh and while the actual compiler usually is a single program, it's unusual to invoke it directly. Instead there's a "front-end" program (like `gcc` or `g++` for GCC) that invokes the actual compiler and then possibly the linker. – Some programmer dude Aug 29 '18 at 09:06
-
1Any of this is specific to exactly which compiler you're using. None of it is specified by the C++ standard. – xaxxon Aug 29 '18 at 09:27
-
No, your comments are not correct. Different compilers do this differently. The C and C++ standards each specify a number of translation phases, one of which is essentially the preprocessor. And, in a number of compiler suites, the preprocessing phase is/was performed by a separate program from other phases. However, that is an implementation choice, and a number of vendors have made different implementation choices. – Peter Aug 29 '18 at 10:43
1 Answers
There is nothing saying how a compiler should/must be designed internally, so it will be different from compiler to compiler.
Traditionally however, they are divided into pre-processor, compiler and linker. These may be inside the same executable, or in several.
The pre-processor does everything that needs to be done before the compiler starts checking the actual language syntax. The compiler is checking if the source is valid C, does it make sense? And then translates the source code into an executable program, which behaves as specified by the C standard.
Some compilers give the output in the form of a hardware-independent "object file", in which case a linker is needed to translate one or several object files into machine code. But the compiler may as well give the output as machine code, in which case the only task of the linker is to put all different parts (translation units) of the program together, including libraries, and check if all identifiers (variables, functions etc) used by the program are present - if not, you will get a linker error.
The C standard (C17 5.1.1.2) only speaks of translation phases, which is mostly dictating the order of pre-processing. Roughly, translation phases 1 to 6 are what we would call "pre-processing", step 7 is "compiling", and step 8 is "linking".

- 195,001
- 40
- 254
- 396
-
most modern compilers generate the assembler output which is assembled to the object file file by assembler proram – 0___________ Aug 29 '18 at 09:53
-
@P__J__: Such as? Only GCC does, AFAIK. It's a slow step, serializing the textual representation of the instructions only for the assembler to turn it back into binary. MSVC for instance doesn't bother. – MSalters Aug 29 '18 at 11:46