Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Header files: Multiple Inclusion problem

 by Sivaraman Dhamodaran on Jan 03, 2012

In this blog we will look at the problem of Multiple header file inclusion
Comments: 0 Views: 913 Printable Version 

The #include is the pre-processor and the statement is replaced by the file content before the actual compilation takes place. This leads to situation of getting declaration or definition twice in a file say ab.cpp when is includes a header file that includes the one more header file which used by the file ab.cpp already.

To explain this, let us go with a simple example.

4.1 SimpleMath.h

Below is the content of the file and no explanation is required her as it is a two simple function declaration and its implementation.

int Add_Numbers(int, int);
int Mult_Numbers(int, int);
int Add_Numbers(int a, int b)
{
            return (a + b);
}

int Mult_Numbers(int a, int b)
{
            return (a * b);

}

4.2 ExtendedMath.h

In this header file the basic mathematic function to add two numbers is extended to support adding three numbers. In this file, the function to add three numbers is implemented by making use of the function that adds two numbers, which is already defined in the SimpleMath.h. So ExtendedMath.h header file includes the simplemath.h header file. This file content is shown below:

#include "SimpleMath.h"
int Add_three_numbers(int, int, int);
int Add_three_numbers(int a, int b, int c)
{
            return ( Add_Numbers(a, b) + c );
}

Explanation
When the pre-processing (before the compilation) takes place, the #include is replaced with file content by the compiler. Also note that the compiler will not generate any object code for the header file say SimpleMath.obj, extendedmath.obj


4.3 CppTest.cpp

Do you got confused in previous section when I said compiler will not generate any object code for the header file say SimpleMath.obj, extendedmath.obj? And you may have question that each header file provided some processing that computes multiplication as well as addition. Where is the object code? And How do the linker will create the exe for those functions? 

Right! Compiler will generate object code for the CPP files. When the CPP file includes the header files, then file has the replaced content of the header and that replaced content will go into object file and linker will generate the exe. Look at the code for the CPP file:

 // CPPTST.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "SimpleMath.h"
#include "ExtendedMath.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
            return 0;

}

 

When you compile the above file or the project, which has this cpp file, you will get the error shown below:

Pic02.JPG

 Why? 

We will see that reason in the next blog.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
Nevron Gauge for SharePoint
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Systems Engineer
Joined: Oct 21, 2010
Education: Masters Degree
 More Blogs from this Blogger
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
Header file inclusion techniques
View all »
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.