compilation process

D

Davor

Hi!

I have a few questions about compilation of C programs.
What does the compiler do after I send him preprocessed file and what
happens from then on? What does the linker do ? Do the libraries contain
C-code, or are they in a binray form?
For exapmle, when I'm compiling a simple program on linux, and I use
sqrt, can I only write #include <math.h>, or do I have to link against
math library too, gcc myprog.c -o myprog -lm ? I'm not quite clear with
this, so any comments and answers are welcome, tutorials and HOW-TOs, or
anything like that is welcome.

Thanks in advance

Davor
 
J

Joona I Palaste

Davor said:
I have a few questions about compilation of C programs.
What does the compiler do after I send him preprocessed file and what
happens from then on? What does the linker do ? Do the libraries contain
C-code, or are they in a binray form?

Implementation-defined. Usually they are in binary form.
For exapmle, when I'm compiling a simple program on linux, and I use
sqrt, can I only write #include <math.h>, or do I have to link against
math library too, gcc myprog.c -o myprog -lm ? I'm not quite clear with
this, so any comments and answers are welcome, tutorials and HOW-TOs, or
anything like that is welcome.

It is generally NOT enough to #include <math.h>. Including headers only
tells the compiler how to CALL the functions defined in the library. It
doesn't tell anything about the actual CODE in the functions. With gcc,
you have to use the -lm switch.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Products like that make me wish I could menstruate."
- Andy Richter
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
I have a few questions about compilation of C programs.

Most of them are off-topic...
What does the compiler do after I send him preprocessed file and what
happens from then on?

It translates the C-source into machine code. Actually, it does its best, but
some addresses are left (and noted) 'unresolved'.
What does the linker do ?

It tries so solve the unrersolved addresses.
Do the libraries contain
C-code, or are they in a binray form?
Binary.

For exapmle, when I'm compiling a simple program on linux, and I use
sqrt, can I only write #include <math.h>, or do I have to link against
math library too, gcc myprog.c -o myprog -lm ?

Yes, you have to link the math library. The headers only include the
prototypes and some constants and types definitions. The executable code is
in the library.
I'm not quite clear with
this, so any comments and answers are welcome, tutorials and HOW-TOs, or
anything like that is welcome.

www.google.com is your friend.
 
J

Jack Klein

Hi!

I have a few questions about compilation of C programs.

The C standard defines eight phases of translation. They are very
specific, but are relatively high level. They are not actually
required to be performed step-by-step, it is quite possible for a
compiler to combine several together as long as the results are the
same.
What does the compiler do after I send him preprocessed file and what
happens from then on? What does the linker do ? Do the libraries contain
C-code, or are they in a binray form?

These details are not specified by the C standard. Things like object
and executable file formats, linkers, and how libraries are managed
are very specific to the operating system and compiler you use. The
standard specifies what the results must be, not the individual
intermediate details.
For exapmle, when I'm compiling a simple program on linux, and I use
sqrt, can I only write #include <math.h>, or do I have to link against
math library too, gcc myprog.c -o myprog -lm ? I'm not quite clear with
this, so any comments and answers are welcome, tutorials and HOW-TOs, or
anything like that is welcome.

Thanks in advance

Davor

The last part, about needing to manually link the math library with
most UNIX/Linux compilers, is basically a left-over annoyance that has
been inexcusable for the at least the last 10 years. But that is also
something specific to the tool set you are using.

There are draft copies of the current C standard in several formats
available at http://anubis.dkuug.dk/JTC1/SC22/WG14/www/docs/n869/,
where you can read about the high level phases of translation the
language defines.

If you want more details on how gcc or any other specific compiler
actually performs its work, you need to take the question to a support
group for that particular compiler.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top