"jugal_panchal" asked:
> here u have built a header file which have all declaration 
> and .cpp
> file for defination but if we will use this outside world 
> the we
> have to only include .h file but my question is that
> how to jump header file to defination file. there is no 
> one way.we
> are useing only header file and in header file no one 
> other file
> (defination file).and in header file we will write in 
> declaration
> file....the way we are using is reverse so can u explain 
> how to
> link.I am not getting this thing in my program also it 
> works but not
> getting myself.....

I'm not sure exactly what you're asking but I hope the 
following helps:

A multi-file project is usually built using a tool like 
'make'. The associated 'makefile' contains rules describing 
how the executable is to be made.

Typically the rule for the executable is to invoke a 
link-loader that stitches together a number of object files 
(*.o). Let's consider a project with just two object files, 
first.o and second.o.

Typically the rule for building an object file is to compile 
the corresponding .c source file. So first.o is made by 
compiling first.c, and second.o is made by compiling 
second.c. This is spelled out in the makefile.

In our project, each of the .c files has a corresponding .h 
file. Now suppose first.c contains

  #include "second.h"

The compiler literally includes this file into first.c, i.e. 
it copies and pastes source code text from second.h into the 
appropriate place in first.c. (If your compiler will let you 
run just the preprocessor, try it; you'll see what I mean. 
In gcc it's the -E option. Warning: it can generate a lot of 
source code if there are library header includes!)

Now let's suppose that first.c calls a function func() 
that's declared in second.h (and defined in second.c). The 
prototype of func() will be pasted into first.c before 
compilation. After compilation, the resulting object file, 
first.o, will contain calls to func(), even though the 
compiler doesn't know its actual definition at that point. 
So the object file is not complete on its own.

The other object file, second.o, is produced in a similar 
way and contains the code for func(). Again, it's not 
complete on its own.

Now for the clever bit. When the link-loader builds the 
executable, it loads both the object files, and links them 
together appropriately. Calls to func() are linked to the 
executable code for func() and so on. If this can't be done, 
error messages are emitted.

(This process is usually hidden from the user, because the 
"compiler" is actually a wrapper for several programs, 
including a preprocessor, compiler front end, assembler, and 
link-loader.)

In the case of library header includes (e.g. stdio.h), the 
corresponding object files (e.g. stdio.c) have already been 
compiled into collections called libraries. The usual 
procedure is to use dynamic linking, i.e. the links to 
library functions are made at run time rather than compile 
time. This makes for a smaller executable. But if static 
linking is used, the library code is added into the 
executable.

I hope this explains in outline what's going on and answers 
your question.

Disclaimer: I'm not a computer scientist and I've "picked 
this up on the streets". So there may be egregious errors in 
the above, which I'm sure better-informed folks will jump 
upon in short order. :-)

David

Reply via email to