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

difference in including the header files

P: n/a
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<iostream.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<iostream.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:+919885534944

Nov 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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_INCLUDED_
#define _THIS_FILE_IS_INCLUDED_

// contents of the header file
#endif

So if you include this file once, then _THIS_FILE_IS_INCLUDED_ 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

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

P: n/a

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

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

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

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

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

P: n/a
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_INCLUDED_
#define _THIS_FILE_IS_INCLUDED_


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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.