473,731 Members | 2,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference in including the header files

Hi c++ Gurus,
Need your blessing.
while testing few aspects with respect to header file inclusions, i
observed few things which i would like to share with you.
i have a file sqlca.h in which a structure sqlca is declared.

i included this file as a soft link in /usr/include.
the soft link is as follows:
sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h

then i wrote a small program as below:

//test1.cpp
#include<iostre am.h>
#include<sqlca. h> // normal inclusion
#include<sqlca. h> // testing for duplicate
int main()
{
return 0;
}

with this kind of inclusion , i didnt get any warnings/errors.

But if i included this file as a soft link in myproject/src/include
and changed the code as shown below:

//test2.cpp
#include<iostre am.h>
#include "sqlca.h" // normal inclusion
#include "sqlca.h" // testing for duplicate
int main()
{
return 0;
}
this gives me a error saying redeclaration of "struct sqlca" in
test2.cpp at 4 line
previously declared in
test2.cpp at 3 line
But the same doesnt throw any error if i create a soft link in
/usr/include.

i am using gcc compiler gcc3.3.2

why is it that :
if we include "/usr/include/sqlca.h" twice in our program doesnt throw
any error message,
where as if we include "myproject/src/include/sqlca.h" throws error.
how the compiler interpretes header files of standard header and normal
headers.
please help me in understanding this.

Thanks & regards
Nagaraj Hayyal
mobile:+9198855 34944

Nov 22 '05 #1
8 3049
nrhayyal wrote:
why is it that :
if we include "/usr/include/sqlca.h" twice in our program doesnt throw
any error message,
where as if we include "myproject/src/include/sqlca.h" throws error.
how the compiler interpretes header files of standard header and normal
headers.


Typically the header files have "guards" like this :

#ifndef _THIS_FILE_IS_I NCLUDED_
#define _THIS_FILE_IS_I NCLUDED_

// contents of the header file
#endif

So if you include this file once, then _THIS_FILE_IS_I NCLUDED_ gets
defined. So next time if you include the same file, the #ifndef
directive evaluates to false. As a result, the contents of the header
file are not copied again.

This is a typical method to avoid multiple inclusions. You can write
this to your header and it would stop giving errors.

Hope this helps.

Nov 22 '05 #2
hi Neelesh,
thanks for ur quick response.
the file which i mentioned contains what you mentioned.
my question was, if the same file is included in "/usr/include/", then
i dont get any error message on including the same file twice.
But if the same file is included in "mypoject/src/include/", then i
get error message on including the same file twice.
there is a difference in compiler reading files from /sur/include and
other user defined header files.
i wanted to know about that.

thanks
Nagaraj Hayyal

Nov 22 '05 #3

nrhayyal wrote:
hi Neelesh,
thanks for ur quick response.
the file which i mentioned contains what you mentioned.
my question was, if the same file is included in "/usr/include/", then
i dont get any error message on including the same file twice.
But if the same file is included in "mypoject/src/include/", then i
get error message on including the same file twice.
there is a difference in compiler reading files from /sur/include and
other user defined header files.
i wanted to know about that.

thanks
Nagaraj Hayyal


Your original question was about a difference between

#include "file.h"

and

#include <file.h>

Those two are differenct and so could lead to different results, but
the differences are implementation-defined so you are going to have to
ask a group specific to your compiler.

Gavin Deane

Nov 22 '05 #4
* nrhayyal:
Need your blessing.
while testing few aspects with respect to header file inclusions, i
observed few things which i would like to share with you.
i have a file sqlca.h in which a structure sqlca is declared.

i included this file as a soft link in /usr/include.
the soft link is as follows:
sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h

then i wrote a small program as below:

//test1.cpp
#include<iostre am.h>
<iostream.h> is not a standard C++ header. Use <iostream>. Note the
difference.

#include<sqlca. h> // normal inclusion
#include<sqlca. h> // testing for duplicate
int main()
{
return 0;
}

with this kind of inclusion , i didnt get any warnings/errors.
That is a compiler-specific result.

It _seems_ that your compiler automatically refrains from actually
including a standard header twice in the same compilation unit (that
would help compilation time by saving disk accesses), and also regards
all headers in the [/usr/include/] directory as standard headers.
But if i included this file as a soft link in myproject/src/include
and changed the code as shown below:

//test2.cpp
#include<iostre am.h>
#include "sqlca.h" // normal inclusion
#include "sqlca.h" // testing for duplicate
int main()
{
return 0;
}
this gives me a error saying redeclaration of "struct sqlca" in
test2.cpp at 4 line
previously declared in
test2.cpp at 3 line
But the same doesnt throw any error if i create a soft link in
/usr/include.

i am using gcc compiler gcc3.3.2

why is it that :
if we include "/usr/include/sqlca.h" twice in our program doesnt throw
any error message,
where as if we include "myproject/src/include/sqlca.h" throws error.
That's because you haven't used a proper include guard in your header
file.

Here's a typical include guard construction:

#ifndef SQLCA_H
#define SQLCA_H

... contents of header file
#endif

A typical newbie error is to add an underscore at the front of the
include guard symbol, like _SQLCA_H_, which produces a symbol reserved
for the C++ implementation. Another typical error is to use a double
underscore somewhere (ditto). And a third, not formally an error but
Bad Practice, to not use ALL UPPERCASE for the macro name.

how the compiler interpretes header files of standard header and normal
headers.


The standard only specifies that "xxx" can add additional searching
compared to <xxx>; there's nothing about include once only.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #5
> A typical newbie error is to add an underscore at the front of the
include guard symbol, like _SQLCA_H_, which produces a symbol reserved
for the C++ implementation.


Confused a bit here.
I am working with g++ 3.4.2 and observe that all the Standard library
header files have guards with a leading underscore. (eg.
_GLIBCXX_VECTOR )
Is this to be considered an error/ bad-practice as well? Or is this
because these are files in the "standard library" ?

Nov 22 '05 #6
* Neelesh Bodas:
A typical newbie error is to add an underscore at the front of the
include guard symbol, like _SQLCA_H_, which produces a symbol reserved
for the C++ implementation.


Confused a bit here.
I am working with g++ 3.4.2 and observe that all the Standard library
header files have guards with a leading underscore. (eg.
_GLIBCXX_VECTOR )
Is this to be considered an error/ bad-practice as well? Or is this
because these are files in the "standard library" ?


It's because they're in the standard library. That's what reserved to
the implementation means. Namely that the C++ implementation can use
them, freely, not risking conflict with any names _you_ might define.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #7
nrhayyal wrote:
my question was, if the same file is included in "/usr/include/", then
i dont get any error message on including the same file twice.
But if the same file is included in "mypoject/src/include/", then i
get error message on including the same file twice.
there is a difference in compiler reading files from /sur/include and
other user defined header files.
i wanted to know about that.


Where files get included from and how, is implementation-defined. You
need to ask in the newsgroup that talks about your compiler.

V
Nov 22 '05 #8
On 18 Nov 2005 03:58:19 -0800, "Neelesh Bodas"
<ne***********@ gmail.com> wrote in comp.lang.c++:
nrhayyal wrote:
why is it that :
if we include "/usr/include/sqlca.h" twice in our program doesnt throw
any error message,
where as if we include "myproject/src/include/sqlca.h" throws error.
how the compiler interpretes header files of standard header and normal
headers.


Typically the header files have "guards" like this :

#ifndef _THIS_FILE_IS_I NCLUDED_
#define _THIS_FILE_IS_I NCLUDED_


Typically you do not, if you know proper programming in C++. It is
illegal for you to define a symbol like this in your code -- all
identifiers beginning with an underscore followed by an upper case
letter, or containing two consecutive underscores anywhere within
them, are reserved for the implementation in all contexts.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
20071
by: RHNewBie | last post by:
Hello, What is the difference between null and NULL. Are x == null and x == NULL the same? The compiler is gcc 3.2. Thanks.
9
13110
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 am curious how compiler find it.
11
1771
by: Pradyut | last post by:
what's the difference between a .h and a .hpp file Also please tell me something about .rh files -- Pradyut http://pradyut.tk http://groups.yahoo.com/group/d_dom/ http://groups-beta.google.com/group/oop_programming India
31
3602
by: Joseph Wakeling | last post by:
Hello all, I'm writing some programs that will be using modules from the GNU Scientific Library (GSL). Include commands within the GSL modules have commands such as, #include <gsl/gsl_rng.h> (for example). Yet these header files aren't actually in the gsl
23
1912
by: yezi | last post by:
Hi, all: The 1st sendtence: int main(){ char string={""}; string = {" connected "}; ..... }
6
1613
by: Al-Burak | last post by:
I have a class which only purpose is to provide services to a variety of classes in other files. The 'manipulator' class is aware of the other classes only because the header files have been include in its header file. However, there are times when some of the other classes are not and will not be dealt with, thus the need to include the header files does not arrive. To handle this, I have used compiler preprocessors to prevent the...
4
6956
by: 'Mani | last post by:
Hi, This is just a generic question, where i want to know what is the difference in including a header file in a .h file and .cpp file. I have a class called MyClass (MyClass.h & MyClass.cpp). There is another class (OtherClass.h & OtherClass.cpp) OtherClass.cpp has a forward declaration to a class called 'Calc' which is in the namespace called 'Utils' like below:
3
2146
by: artifact.one | last post by:
Hi. What's the practical difference between: #include <header.h> and: #include "header.h"
3
2572
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c files). I know that there are two ways of specifying include path of header files
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9303
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9177
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4803
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.