473,387 Members | 1,502 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,387 software developers and data experts.

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

Similar topics

24
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
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...
11
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/...
31
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...
23
by: yezi | last post by:
Hi, all: The 1st sendtence: int main(){ char string={""}; string = {" connected "}; ..... }
6
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...
4
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)....
3
by: artifact.one | last post by:
Hi. What's the practical difference between: #include <header.h> and: #include "header.h"
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.