Hello,

i am a naive in C/C++ and pretty confused about how does the compilation process work in the background, can you explain in detail (or give me a link about it) ? in particular can you please tell me: how are declarations in header file associated with the definitions in the .CPP file??Basically, I don't understand how a header file being included makes a connection with the source file that has the definitions for whatever is in the header file. It is my understanding I could name my header files something completely
unrelated to the source files which contain the definitions and including the header files would still make the defined functions/whatnot usable. How?

how are declarations in header file associated with the definitions in the .CPP file??

Because you #include "myheader.h" at the top of the cpp file. #include tells the pre-processor to bung the code in "myheader.h" right there.

Yes you can call the header whatever you like, but why would you? it's not logical and programming is all about logic.

Why are they separate?
Because if you make flexible reuseable code (see my comments about being logical) it can be pre-compiled into a libray and used directly in other projects without having to be re-compiled for each project. The header file remains in its "plain text" state so the user of your library can see what methods are vailable and what they return etc. without having to skirt arround reams of code.

the compiler process basically is:

Pre-process (#include, #define, #ifndef macros etc...)
Compile .cpp into .o object files
link object files to an .exe or .dll blah...

The content of header file is prepended to source file.

>Basically, I don't understand how a header file being included makes a connection
>with the source file that has the definitions for whatever is in the header file.
It doesn't. There's no connection at all. The header is there to declare names so that you can compile your source. If the names aren't declared, you'll get compilation errors:

#include <stdio.h>

int main ( void )
{
  x = 123; /* This line won't compile */

  printf ( "%d\n", x );

  return 0;
}

But if you declare the name, even if the object itself wasn't defined, the compiler won't complain:

#include <stdio.h>

extern int x; /* Declared but not defined */

int main ( void )
{
  x = 123; /* Now this line compiles */

  printf ( "%d\n", x );

  return 0;
}

When you try to build this code, the linker will start complaining because x was only declared, never defined. That's a different issue entirely. You can add another file that defines x and everything will work when you compile and link them together (no headers involved):

/* File 1 */
#include <stdio.h>

extern int x; /* Declared but not defined */

int main ( void )
{
  x = 123; /* Now this line compiles */

  printf ( "%d\n", x );

  return 0;
}
/* File 2 */
int x;

All a header does is provide declarations. It doesn't know about the linker, or any implementation files. It declares names and doesn't give a hooey about whether they're defined or not. That's the job of the linker. So, you can throw the extern int x; part into a header and call it good:

/* Header file */
extern int x; /* Declared but not defined */
/* File 1 */
#include <stdio.h>
#include "header.h"

int main ( void )
{
  x = 123; /* Now this line compiles */

  printf ( "%d\n", x );

  return 0;
}
/* File 2 */
int x;

The #include direction just replaces itself with the contents of the file you specify, so after preprocessing header.h, file 1 looks just like this (which is functionally identical to what we did before):

/* File 1 */
#include <stdio.h>
/* Header file */
extern int x; /* Declared but not defined */

int main ( void )
{
  x = 123; /* Now this line compiles */

  printf ( "%d\n", x );

  return 0;
}

That's all there is to it.

Hmm ok...now i have understood it all.Thanks to all of you!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.