473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

#include<> internals

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
5 1576
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Tuckers | last post by:
My question is, if I have created my own library which lives in its own install directory, to refer to its header file is it better to use #include "MyLibrary.h" or #include <MyLibrary.h> ...
9
by: bill | last post by:
Forget the exact definition of difference between, #include <foo.h> and #include "bar.h" Normally foo.h is a standard header file, so it's path is not defined in compiler option, but I...
2
by: Jofio | last post by:
I have 3 files, namely: dArray.h dArray.cp TestdArray.cpp Problem is when I compile the 'main' program - TestdArray.cpp - , it (the compiller) produces the following error: 'Unable to open...
12
by: Pablo Suarez | last post by:
When I code #include "myheader.h" then this header file is searched in the current directory. But where does the compiler search the header file when I write #include <myheader.h>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.