By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

#include<> internals

P: n/a
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

Dec 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ma********@gmail.com wrote:
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
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 13 '05 #2

P: n/a
On Tue, 13 Dec 2005 01:36:59 -0600, Artie Gold wrote:

<snip>
(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.

Dec 13 '05 #3

P: n/a

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

Dec 13 '05 #4

P: n/a
On 2005-12-13, Mi*************@tomtom.com
<Mi*************@tomtom.com> wrote:

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.)


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

--
Neil Cerutti
Dec 13 '05 #5

P: n/a
Artie Gold wrote:
ma********@gmail.com wrote:
Header1.h
int g_header1 = 10;

Source1.C/CPP code
#include <header1.h> Source2.C/CPP code
#include <header1.h>

1. Internally, during compilation of the code, what the compiler does?
2. How many instances of g_header1, g_header2 would be there?
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.

Dec 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.