home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] Re: Parsing when compiling C - generalized understanding question?

  • Subject: Re: [OCLUG-Tech] Re: Parsing when compiling C - generalized understanding question?
  • From: Charles MacDonald <cmacd [ at ] telecomottawa [ dot ] net>
  • Date: Thu, 29 Sep 2005 23:05:36 -0400
William Case wrote:
I am no Geek, and so I have but a fussy notion of some of these, but I
will throw out my notion of what happens, that way the real geeks will
jump in!

> Somewhere in that process makefile must actually compile the various
> files, yet when I looked in the default makefile I could not see a
> command that would start compilation.  Does the default makefile use
> gcc, or, another perhaps built-in compiler?

The Makefile is "run" by make, which knows by default that to turn a .c
file into a .o file It has to use GCC with some options, You can put a
special version in your makefile if you want to use some other compiler
for example.

> Now, suppose I got the compiler going (I have been using gcc -g foo.c -o
> foo). I then have the following questions:
> Quoting from 'info gcc'.
> "Compilation can involve up to four stages, always in the following
> order:
> *preprocessing
> *compiling
> *assembling
> *linking

> I suppose from the above, preprocessing finds and/or adds the files in
> #include or replaces all the constants with the name with the value
> given in #define  value?
> I am assuming that any #statements are what are called macros and the
> preprocessor takes care of macros?
> If the gcc preprocessor is built-in when would I use cpp?

If you have a cpp file, it will also be preprocessed, and made ready to

> "compiling produces an object file,"

> In a few words, what is optimization -- all the blanks are gone; all the
> syntax has already been rearranged?

As the code is compiled, it gets translated into the language/machine
independent internal format, probably more as an object in memory than a
text file.  That is then converted to assembly language, which you can
get out by giving the gcc a directive.

One you have assembly code, it is assembled, and then the .asm (or is it
.a) file is deleted as it is not needed.

In the process of compiling, you code will be sliced and diced to make
it more efficient, just compare the output in assembly with your
original code.  In fact rewrite the program to be more or less
efficient, and see if the assembly changes.!

> It sounds like the preprocessor, compiler and linker do everything, why
> do I need the assembler?

The compiler gives you assembly code, which must be assembled.

> If I print the file after the assember stage, I can see my source code
> changed into assembler code, can't I?  Shouldn't I see assembler code
> after the compiler is finished?

Normally deleted, you can ask for it.

> "The last stage, linking, completes the compilation
> process, combining all object files (newly compiled, and those
> specified as input) into an executable file."
> Is this where the code (after being twisted into a meaningful lexicon
> and syntax, or assembler code) finally gets turned into binary?

It is where the Binary .o modules are wrapped up and made ready to run,
(linking in the code that starts the program, sets up the machine and
eventually calls _main()   )

> Is this when the functions referenced through the header #include get
> added to the compiled program?

No, they are compiled every time....

> Is this just a reference or is the actual binary for the function added
> into the compiled file?
> Isn't there something about dynamically linked files (dll) that applies
> here? What are they in relation to a compiled program?

I think you have to specify if you want the library files "hard linked"
otherwise, it will just connect to the external libs as part of the code
that runs before you get to call _main()

Charles MacDonald      cmacd [ at ] TelecomOttawa [ dot ] net     Stittsville Ontario
1800+ Canadians oppose Bill C-60 which protects antiquated Recording,
 Motion Picture and "software manufacturing" industries from change...