#include<> internals

M

mailtogops

Hi All,

This is very basic C/C++ question but I need internals of C/C++ how the
langauge/compiler do this..

Say I have two header files

1. header1.h
2. header2.h

Here the code for header1.h

Header1.h
---------------
int g_header1 = 10;

void print1()
{
printf("%d",g_header1);
}

Here the code for header2.h

Header2.h
----------------

int g_header2 = 10;

void print2()
{
printf("%d",g_header1);
}


Now I have two C/CPP source files

They are

1. Source1.C/CPP
2. Source2.C/CPP

Source1.C/CPP code

#include <header1.h>
#include <header2.h>

void Disp()
{
g_header1 = 400;
g_header2= 300;
print1();
print2();
}

Source2.C/CPP code

#include <header1.h>
#include <header2.h>

void DispResult()
{
g_header1 = 1000;
g_header2= 2000;
print1();
print2();
}

Here assumption is all the four files are in the same project

My Question

1. Internally, during compilation of the code, what the compiler does?
2. How many instances of g_header1, g_header2 would be there?

I would appreciate if anyone clear my long time uncleared question..

Thanks & Regards,

Gopal
 
A

Artie Gold

Hi All,

This is very basic C/C++ question but I need internals of C/C++ how the
langauge/compiler do this..

Say I have two header files

1. header1.h
2. header2.h

Here the code for header1.h

Header1.h
---------------
int g_header1 = 10;

void print1()
{
printf("%d",g_header1);
}

Here the code for header2.h

Header2.h
----------------

int g_header2 = 10;

void print2()
{
printf("%d",g_header1);
}


Now I have two C/CPP source files

They are

1. Source1.C/CPP
2. Source2.C/CPP

Source1.C/CPP code

#include <header1.h>
#include <header2.h>

void Disp()
{
g_header1 = 400;
g_header2= 300;
print1();
print2();
}

Source2.C/CPP code

#include <header1.h>
#include <header2.h>

void DispResult()
{
g_header1 = 1000;
g_header2= 2000;
print1();
print2();
}

Here assumption is all the four files are in the same project

The term `project' does not relate to the C++ language. It is a creature
of an IDE of some kind.
My Question

1. Internally, during compilation of the code, what the compiler does?
2. How many instances of g_header1, g_header2 would be there?
The preprocessor works by textual replacement. The next stage of the
compiler "sees" what the preprocessor generates -- known as a
translation unit -- and acts upon it.

In the case of `Source1', for example, what the compiler really `sees' is:
> int g_header1 = 10;
>
> void print1()
> {
> printf("%d",g_header1);
> }
> int g_header2 = 10;
>
> void print2()
> {
> printf("%d",g_header1);
> }
> void Disp()
> {
> g_header1 = 400;
> g_header2= 300;
> print1();
> print2();
> }

(What Source generates is left as an exercise for the poster.)

Multiply defined variables will get kicked out by the linker.

This is why you do *not* put definitions in headers (there are some
exceptions like const integral types which are implicitly static, but
you get the idea). You put *declarations*: Function prototypes.
Templates. extern variable declarations. *Not* definitions.

HTH,
--ag
 
K

Kleuskes & Moos

On Tue, 13 Dec 2005 01:36:59 -0600, Artie Gold wrote:

(What Source generates is left as an exercise for the poster.)

Multiply defined variables will get kicked out by the linker.

This is why you do *not* put definitions in headers (there are some
exceptions like const integral types which are implicitly static, but
you get the idea). You put *declarations*: Function prototypes.
Templates. extern variable declarations. *Not* definitions.

Additionally, it's a good idea to use guards in header files

<code>
#ifndef GUARD_FOOBAR
#define GUARD_FOOBAR

// your declarations go here.

#endif
</code>

This will ensure the content of your header is parsed only once.
 
M

Michiel.Salters

Kleuskes & Moos wrote:
it's a good idea to use guards in header files
<code>
#ifndef GUARD_FOOBAR
#define GUARD_FOOBAR

// your declarations go here.

#endif
</code>

This will ensure the content of your header is parsed only once.

Once per translation unit (which usually means every .cpp file included
in your makefile/project/, or .cxx file - see your compiler manual).
If you have two TU's, each TU can contain the header once, but
functions defined in a header might be defined twice then. This may
still upset the linker, despite the header guards. (if the functions
are
inline, this doesn't matter.)

HTH,
Michiel
 
N

Neil Cerutti

Kleuskes & Moos wrote:
it's a good idea to use guards in header files

Once per translation unit (which usually means every .cpp file
included in your makefile/project/, or .cxx file - see your
compiler manual). If you have two TU's, each TU can contain the
header once, but functions defined in a header might be defined
twice then. This may still upset the linker, despite the header
guards. (if the functions are inline, this doesn't matter.)

Are there strategies to make headers idempotent in C++ without
the use of include guards?
 
O

Old Wolf

Multiply defined variables will get kicked out by the linker.

Actually they cause undefined behaviour. This situation also
commonly results in no linker error and there being two variables
defined; or with some types of linker, one variable defined and both
identifiers refer to it.

Adding include guards (as suggested by other posters) is
a good idea but it won't solve this problem.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top