473,473 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

querry about compiling and linking

Hi this is gurpreet,
I know this is a very simple question but still I want
to clear some doubts.
What happens when we compile and link a c-program?
I hope aquite a lot of responses to my querry.
BYE! BYE!
Nov 14 '05 #1
2 1253
gurpreet <ap***********@yahoo.com> scribbled the following:
Hi this is gurpreet,
I know this is a very simple question but still I want
to clear some doubts.
What happens when we compile and link a c-program? I hope aquite a lot of responses to my querry.


It depends entirely on the implementation.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Keep shooting, sooner or later you're bound to hit something."
- Misfire
Nov 14 '05 #2
In article <af**************************@posting.google.com >,
gurpreet <ap***********@yahoo.com> wrote:
: I know this is a very simple question but still I want
:to clear some doubts.
:What happens when we compile and link a c-program?

The ANSI/ISO specification lists a series of "phases" of compilation.
You pretty much have to read the phase description for yourself to
understand all the implications.

If one takes a program its most basic form, without preprocessor
magic (which is important magic) and without comments or oddities
such as trigraphs, and without \ sequences in quoted strings,
then -generally- speaking, the compiler examines sequences of
characters, internally breaks them up at "token" boundaries
(e.g., breaks 2*-36 into "2", "*", "-" and "36") and then starts
examining the sequence of tokens.

There is more than one major method of figuring out what the program
"means" from the sequence of tokens. In one of the methods, the
compiler proceeds token by token, with each new token encountered
causing a transition to a new state which is dependant on the token.

For example, upon seeing the "2" token it would enter the state
corresponding to "I have a number". Then when it saw the "*" it would
say "'*' can mean pointer indirection or multiplication, but pointer
indirection is not valid right after a number, so I must have a
partially-completed binary term." Then it would see the "-" and say
"'-' can mean subtraction or unary negatation, but subtraction is not
valid at this point in a binary term, so it must be negation". Upon
seeing the "36" it would say "Okay, that's a number" and then "Okay,
now I know what was being negated" and would take note of that, but it
would -not- immediately record the -36 as the second part of the term.
It would instead look further on, see that there is no more input: then
it would know that it was not, for example, part way through 2*-36/6
in which the '/' needs to be done first. Knowing that there was
nothing following the -36, it would look at it's state and say
"Okay, so the negated number must be the second part of the term
that I'm in the middle of processing, and then would take a record
that it had figured out that you were multiplying two things together,
the first of which is the number 2 and the second of which is the number
-36. After that, it would look again, see there was no more input
and see that there is no pending action such as an assignment,
and so it would figure out that you must be using the C statement
which is an expression by itself (evaluated for its side effects).

At that point it might look and say "Oh, but an expression statement
is only valid inside a block, and you aren't inside a block" and
so emit a compiler error. But if there was sufficient syntactical
context, then after processing the rest of that context, it could
emit some kind of representation of "multiply" 2 -36 and send that
to the code generator.

The code generation phase gets passed an in-memory representation
of the code to be generated, with everything already figured out as
to any mandatory order of operations -- but possibly without
having already nailed down the order of operation in cases where the
standard leaves the order to the implimentation. The code generator
would usually run one or more optimization phases, such as "dead code
elimination" [in which it is noticed that certain code is never used,
perhaps because it is inside an 'if' statement that is always false.]
Much more sophisticated optimization is possible as well. Once
an optimized internal representation of the code exists, the code
generator would then proceed to produce a platform-dependant sequence
of instructions that would be necessary to run the program. For
example, it might emit some headers and then machine code equivilent
to

load data register #1 with the constant value 2
load data register #2 with the constant value -36
multiply data register #1 by #2, putting the result into the
register pair (#1,#2) [multiplication might produce a result
with twice as many bits as the original values]
ignore the overflow value stored in data register #1
store the value currently in data register #2 to memory location ...

Sometimes more optimization is done after this, especially
"peephole optimization", which does very local optimizations. An
example of a "peephole optimization" would be for the optimizer
to notice that the value being multipled by is a small constant and
notice that the instruction set has a combination multiply
instruction that can be used with small constants in the case
where the overflow is being ignored, and so might
transform the code to something like;

load data register #1 with the constant value 2
multply data register #1 by the constant -36, putting the result
in data register #1
store the value currently in data register #1 to memory location...
After all the code is generated would come the linker phase. There
is more than one way that linkers can do their work, but generally
speaking they look for unresolved variable and function references
in the object code, figure out where those variables or fuhctions
are defined, and then patches up all the code references to use
the appropriate offsets. In some systems, there is a rigid separation
between an "executable" and a mostly-compiled file, with the linker
taking the mostly-compiled files and emitting an "executable".
In other systems, executables and partially-compiled files have the
same format, and as far as the linker is concerned the operation is
just one of taking two partily-compiled files and combining them into
a new composite partially-compiled file; such systems don't notice
that there are missing definitions until you actually tell it to
start executing the file.
--
Will you ask your master if he wants to join my court at Camelot?!
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joonas Paalasmaa | last post by:
Hi, When compiling Sketch's streamfilter C extension the errors below are raised during linking. What could cause the errors? (Python 2.3, MinGw 1.1 with GCC 2.95.3-6, Windows 98) Here are...
5
by: Mike S. | last post by:
Hello, Has anyone had success compiling the informixdb-1.3 module under python 2.2? It seems the absense of makefile.pre.in in python 2.2 causes the break under py2.2. Is there an easy way...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
3
by: c++_Passioner | last post by:
I only know Compiling usede to convert the .cpp file into .obj file. but have really no idea about linking. please let me know about this. Thanking you, Pritam
1
by: fsmountain | last post by:
Hi guys, I am trying to compile VTK5.0 with Qt GUI Support in Windows 2000 that has Qt Open-Sourced 4.1.2 and CMake 2.4.2 installed. After QVTK is enabled in CMake, configuring it will prompt...
5
by: skumar434 | last post by:
Hi everybody, I am faceing problem with strings. The code is given bellow .In this program i am tring to copy data from a file into structure . I am able to copy the data ,but the dat is...
2
by: stevenruiz | last post by:
Hi Everyone, The Strings.h has the function Get_Line which is defined and the error is shown below: Strings.h: void get_line( istream & );
3
by: geoplab | last post by:
I have this error message when compiling mx_init.c by linking libxml library as follow: gcc `xml2-config --cflags` -c mx_init.c `xml2-config --libs` -g -Wall gcc: -lxml2: linker input file...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.