Topic : Compiling C++ Programs On Unix
Author : LUPG
Page : << Previous 3  
Go to page :


and see what the resulting code looks like.
The C Compiler - normally called "cc1". This is the actual compiler, that translates the input file into assembly language. As you saw, we used the "-c" flag to invoke it, along with the C Pre-Processor, (and possibly the optimizer too, read on), and the assembler.
Optimizer - sometimes comes as a separate module and sometimes as the found inside the compiler module. This one handles the optimization on a representation of the code that is language-neutral. This way, you can use the same optimizer for compilers of different programming languages.
Assembler - sometimes called "as". This takes the assembly code generated by the compiler, and translates it into machine language code kept in object files. With gcc, you could tell the driver to generated only the assembly code, by a command like:

cc -S single_source.c


Linker-Loader - This is the tool that takes all the object files (and C libraries), and links them together, to form one executable file, in a format the operating system supports. A Common format these days is known as "ELF". On SunOs systems, and other older systems, a format named "a.out" was used. This format defines the internal structure of the executable file - location of data segment, location of source code segment, location of debug information and so on.

As you see, the compilation is split in to many different phases. Not all compiler employs exactly the same phases, and sometimes (e.g. for C++ compilers) the situation is even more complex. But the basic idea is quite similar - split the compiler into many different parts, to give the programmer more flexibility, and to allow the compiler developers to re-use as many modules as possible in different compilers for different languages (by replacing the preprocessor and compiler modules), or for different architectures (by replacing the assembly and linker-loader parts).




This is some source code, that comes with this tutorial:

single_main.c


#include <stdio.h>

int
main(int argc, char* argv[])
{
    printf ("hello world\n");

    return 0;
}



single_main.cc


#include <iostream.h>

int
main(int argc, char* argv[])
{
    cout << "hello world" << endl;

    return 0;
}



a.c


int
func_a()
{
    return 5;
}


b.c


int
func_b()
{
    return 10 * 10;
}


main.c


#include <stdio.h>

/* define some external functions */
extern int func_a();
extern int func_b();

int
main(int argc, char* argv[])
{
    int a = func_a();
    int b = func_b();
    char c;
    char* bc = &c;

    printf("hello world,\n");
    printf("a - %d; b - %d;\n", a, b);

    return 0;
}




Page : << Previous 3