difference in including the header files

N

nrhayyal

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
 
N

Neelesh Bodas

nrhayyal said:
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.
 
N

nrhayyal

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
 
D

deane_gavin

nrhayyal said:
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
 
A

Alf P. Steinbach

* 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.
 
N

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" ?
 
A

Alf P. Steinbach

* Neelesh Bodas:
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.
 
V

Victor Bazarov

nrhayyal said:
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
 
J

Jack Klein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top