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

header file without include guard

Hi, all:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?
thanks.

Oct 20 '05 #1
11 3981
we*****@yahoo.com wrote:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Nothing. They just reside there.
Are these header files are included more than once?
Included? Who said anything about including them? Oh, you are asking if
you actually write

#include "thename.ext"

in one of your files, what would happen, right? That's compiler-specific.
If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?


Within the same compiler it must be. Refer to your compiler
documentation. RTFM, IOW.

V
Oct 20 '05 #2

we*****@yahoo.com wrote:
Hi, all:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?
thanks.

Only the first found file will be included. So it depends on the
compiler where it will look for a file with that name first. Usualy,
compiler looks for includes in the directories specified in INCLUDE
environment variable, in exact the same order as they are specified
there.

Oct 20 '05 #3
we*****@yahoo.com wrote:
Hi, all:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?
thanks.


The compiler will only see the first header file, and not the second.
The first header file it sees should be the path that is listed first
in the include path.

Oct 20 '05 #4
If so, what is good for include guard?

Oct 20 '05 #5
ben
we*****@yahoo.com wrote:
Hi, all:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?
thanks.


Headers can't have the same name. "hello.h" and "somelibrary/hello.h"
may or may not refer to the same header.

The fact is, only one of the header will be #included and may generate
errors if "ond definition rule" is violated. So it is a good idea always
put header guard in headers.

There's no such thing as overwrite in C++. Only overloading and
specialization. Neither is (directly) related to header inclusions. If a
declaration but not a definition is repeated, it is fine. If a
definition is repeated, you get an error.

Ben
Oct 20 '05 #6
got it, thanks.

Oct 20 '05 #7

wenm...@yahoo.com wrote:
If so, what is good for include guard?


Include guards have nothing to do with a situation where two different
headers of the same name exist.

Do you know why this would be bad if you didn't use include guards?

#include "file1.h" // contains, for example,
// definition of class C
#include "file2.h" // file2.h also includes file1.h

int main()
{
...
}

Gavin Deane

Oct 20 '05 #8
In article <11**********************@o13g2000cwo.googlegroups .com>,
paulius-maruska <pa*************@gmail.com> wrote:
we*****@yahoo.com wrote:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?
thanks.

Only the first found file will be included. So it depends on the
compiler where it will look for a file with that name first. Usualy,
compiler looks for includes in the directories specified in INCLUDE
environment variable, in exact the same order as they are specified
there.


It is not tue that only the first found file will be include'd
(see Victor's respoonse). It's also not the case that usually
the path chosen are taken from the INCLUDE environment variable;
some do, but this is completely compiler specific, and most include
other choices too even if they do use INCLUDE.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 20 '05 #9
we*****@yahoo.com wrote:
If so, what is good for include guard?


They are called "*double* inclusion guards". If I include another header
that includes this one _again_, I won't get redefinitions of everything
and therefore the compilation will proceed normally.

V
Oct 20 '05 #10
In article <11**********************@g43g2000cwa.googlegroups .com>,
<we*****@yahoo.com> wrote:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
It's implementation defined.

This can happen _for instance_ if you are using two 3rd party libs
and they both end up having some common functionality and end up
with the same name, obviously in different locations.
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?


Again, it's implementation-defined.

Probably in the end though, you want to have different names just
to avoid this kind of situation if you have any control over it.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 20 '05 #11
On 20 Oct 2005 11:14:02 -0400, co****@panix.com (Greg Comeau) wrote in
comp.lang.c++:
In article <11**********************@o13g2000cwo.googlegroups .com>,
paulius-maruska <pa*************@gmail.com> wrote:
we*****@yahoo.com wrote:
What will happen if header files with the exact same names but with
different contents and declared/defined without include guard reside in
the include path for the compiler?
Are these header files are included more than once? If so, is it the
lastest included file always overwrite the previous one? Is this
behavior predictable?
thanks.

Only the first found file will be included. So it depends on the
compiler where it will look for a file with that name first. Usualy,
compiler looks for includes in the directories specified in INCLUDE
environment variable, in exact the same order as they are specified
there.


It is not tue that only the first found file will be include'd
(see Victor's respoonse). It's also not the case that usually
the path chosen are taken from the INCLUDE environment variable;
some do, but this is completely compiler specific, and most include
other choices too even if they do use INCLUDE.


I think you are wiggling a little bit too much on this one. A
compiler that included more than one file would be completely
non-conforming, since the standard says "the header" and "the source
file".

There is nothing that requires that an implementation select the first
matching header or file it finds in the implementation-defined search,
it could search the relevant space and use the last matching file or
header. If there is more than one matching header or file to be
found, it could arbitrarily select among them randomly or based on
some unspecified criteria.

The only thing the standard will not allow it to do is directly
include more than one header or file because of one #include
directive.

--
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
Oct 21 '05 #12

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

Similar topics

7
by: Karen | last post by:
Hi, I have one constant variable and want to use it in two files. I put it in the header file and then include the header file. The compiler always say "error C2370: 'arraysize' : redefinition;...
6
by: Johannes Bauer | last post by:
Hi group, I've got a question concerning inclusion of .hpp files. Currently I'm including all needed header files in the .cpp file. This means all dependencies of the package and all...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
8
by: nrhayyal | last post by:
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...
18
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for...
15
by: asdf | last post by:
I am confused by the header guard, for example, #ifndef SALESITEM_H #define SALESITEM_H ..... #endif what is the "SALEITEM_H" ? It is a variable name, or something else? I could not find...
7
by: bcpkh | last post by:
Hello All Received a header file from a supplier that defines an interface to implement but it's giving me a problem, I reproduce the general structure of the header file below; #ifndef XYZ_H...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
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: 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
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,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.