Seperate compilation (header files n' their implementations)

C

Chris Mantoulidis

Seperate compilation (that's what it's called, right?) seems to be
quite popular, so I decided to get some info about it, and (d'oh) use
it...

But it's whole structure seems weird to me...

Here's what I think of how it is (from what I've read):

THE PROJECT
+1st header file
+Implementation of the 1st header file (includes the header file)
+2nd header file
+Implementation of the 2nd header file (includes the header file)
........
+Nth header file
+Implementation of the Nth header file (includes the header file)
+MAIN program which includes the header files

I guess the above structure is correct...

But here's what I don't understand...

Since the main program includes the header files, it only has the
interfaces and not the implementations of the headers (since the
implementations are in the .cc files, and not in the .h files).

But the headers DON'T include their implementations; the opposite
though does happen...

So how can the program be aware of the implementations since it seems
like the .cc files are nowhere included??????

And how would I compile such a project? For example, let's say I
compile the main program... Whatever will happen of the
implementations???

Thanks in advance and for your patience,
cmad
 
V

Victor Bazarov

Chris Mantoulidis said:
Seperate compilation (that's what it's called, right?)

I always thought it was spelled "sepArate compilation"...
seems to be
quite popular, so I decided to get some info about it, and (d'oh) use
it...

What's the "d'oh" about? Did you mean "duh" instead of "d'oh"?
But it's whole structure seems weird to me...

Here's what I think of how it is (from what I've read):

THE PROJECT
+1st header file
+Implementation of the 1st header file (includes the header file)

The correct way of saying would be:

1st header: definition of the 1st class and related declarations
1st translation unit: definitions of 1st class members and other
related objects/functions.
etc.
+2nd header file
+Implementation of the 2nd header file (includes the header file)
.......
+Nth header file
+Implementation of the Nth header file (includes the header file)
+MAIN program which includes the header files

I guess the above structure is correct...

Depends on how you look at it.
But here's what I don't understand...

Since the main program includes the header files, it only has the
interfaces and not the implementations of the headers (since the
implementations are in the .cc files, and not in the .h files).

That's basically what it boils down to, yes.
But the headers DON'T include their implementations; the opposite
though does happen...

Headers _contain_ declarations and class definitions. They don't
include anything.
So how can the program be aware of the implementations since it seems
like the .cc files are nowhere included??????

It's _linked_ at the _linking_ stage of building the program.
And how would I compile such a project?

Just like you said in te beginning, _separately_.
For example, let's say I
compile the main program... Whatever will happen of the
implementations???

You compile them too.

I guess you need a decent book that describes how to use your
compiler/linker/librarian programs. It very much depends on what
compiler/platform you're using. And it pretty much goes beyond the
scope of this newsgroup. I recommend asking in a newsgroup for
your compiler.

Victor
 
O

osmium

Chris said:
So how can the program be aware of the implementations since it seems
like the .cc files are nowhere included??????

The linker knows where the libraries are, it found out by some magic when
the compiler was installed/whatever.
 
J

Jared Dykstra

(e-mail address removed) (Chris Mantoulidis) wrote in message
Since the main program includes the header files, it only has the
interfaces and not the implementations of the headers (since the
implementations are in the .cc files, and not in the .h files).

But the headers DON'T include their implementations; the opposite
though does happen...

So how can the program be aware of the implementations since it seems
like the .cc files are nowhere included??????

And how would I compile such a project? For example, let's say I
compile the main program... Whatever will happen of the
implementations???

Thanks in advance and for your patience,
cmad


It sounds like you lack a real understanding of how a binary is
produced from source code. There are two steps, compilation and
linking.

In compilation, the code is validated by the compiler and translated
into blocks of machine code called object files. The compiler does
not care how the functions in other blocks are implemented. Header
files are the interface between the blocks and the compiler simply
validates how you call a function against how this function is defined
in the header.

The compiler will only complain if it finds a function call and no
matching declaration for this function--for good reason. A mismatch
may indicate a typo or other syntatical error in your code.

Once an object file exists for each block of code, a single executable
is produced. It is at this point where all of the function calls and
implementations are connected--linked--together. If you have a
function that is defined and not used, *and* if you call that function
somewhere in the code it will not be noticed until this step.

There are TONS of resources available to read up on this. Try google.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top