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

Multiple Inclusion Guards Work in MSVC++ .NET...right?

I'm flummoxed. I'm a veteran C++ programmer from the Unix/Linux camp,
trying to learn Visual C++. I'm trying to build a project in which I
need to include one header in a couple of different files, but the
classic multiple inclusion problem is biting me hard. The
#ifndef..#define..#endif method doesn't seem to be working, although
all the documentation I've read indicates that it should.

As a small example, I have an empty console project with three files:
globals.h, functions.cpp and driver.cpp. They look like this:

// ----------------------------------------------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_

const char *msg = "Wiggety wack";

#endif
// EOF

// ----------------------------------------------------
// functions.cpp
#include <iostream>
using namespace std;

#include "globals.h"

void getWiggety ()
{
cout << msg << endl;
}
// EOF

// ----------------------------------------------------
// driver.cpp
#include <iostream>
using namespace std;

#include "globals.h"

extern void getWiggety (void);

int main (void)
{
cout << msg << endl;
getWiggety ();
return 0;
}
// EOF

This won't link, because msg is declared twice, in spite my
#ifndef..#define..#endif in globals.h. I've gone and looked at
<iostream>, and it's protected against multiple inclusion the same way
as I'm doing it. I'm also including it in two places but...the linker
doesn't complain about std::cout et. al.

WTFO?

ff
Jul 22 '05 #1
14 1727
"Fritz Foetzl" <fr**********@hotmail.com> wrote in message
news:d2**************************@posting.google.c om...
I'm flummoxed. I'm a veteran C++ programmer from the Unix/Linux camp, trying to learn Visual C++. I'm trying to build a project in which I
need to include one header in a couple of different files, but the
classic multiple inclusion problem is biting me hard. The
#ifndef..#define..#endif method doesn't seem to be working, although
all the documentation I've read indicates that it should.

As a small example, I have an empty console project with three files: globals.h, functions.cpp and driver.cpp. They look like this:

// ----------------------------------------------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_

const char *msg = "Wiggety wack";

#endif
// EOF

// ----------------------------------------------------
// functions.cpp
#include <iostream>
using namespace std;

#include "globals.h"

void getWiggety ()
{
cout << msg << endl;
}
// EOF

// ----------------------------------------------------
// driver.cpp
#include <iostream>
using namespace std;

#include "globals.h"

extern void getWiggety (void);

int main (void)
{
cout << msg << endl;
getWiggety ();
return 0;
}
// EOF


I think your problem has nothing to do with include guards. The header
"globals.h" is properly included twice, once when each of the files
"driver.cpp" and "functions.cpp" is compiled. The problem is that msg
is being defined twice in the same program, violating the ODR.

You should probably decalre it extern and define it in "global.cpp",
or use an inline function instead

inline msg()
{
static const char* s = "Wiggety wack";
return s;
}

Jonathan
Jul 22 '05 #2
Fritz Foetzl wrote:
I'm flummoxed. I'm a veteran C++ programmer from the Unix/Linux camp,
trying to learn Visual C++. I'm trying to build a project in which I
need to include one header in a couple of different files, but the
classic multiple inclusion problem is biting me hard. The
#ifndef..#define..#endif method doesn't seem to be working, although
all the documentation I've read indicates that it should.

As a small example, I have an empty console project with three files:
globals.h, functions.cpp and driver.cpp. They look like this:

// ----------------------------------------------------
// globals.h
#ifndef _GLOBALS_
#define _GLOBALS_


You need a new idiom for include guard names. Identifiers beginning with
an underscore followed by an upper case letter or another underscore are
reserved for the implementation's use for any purpose. Unless you are
sure you know better, avoid identifiers that begin with an underscore.

This is not your problem, however. Nor is it multiple inclusion. Include
guards protect against the same thing appearing multiple times in a
single translation unit, but not against the same thing appearing in two
different translation units. For example, if I have a main() function in
a.cpp and I also have a main() function in b.cpp, my program won't link.
This is basically what you are doing - you have 'msg' appearing in both
..cpp files, because each has it's own #included copy of globals.h.

The proper way to do this is to only declare 'msg' in the header (with
'extern', and no initialization) and then have its definition (along
with the initialization) in a .cpp file somewhere.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #3
"Kevin Goodsell" <us*********************@neverbox.com> wrote in
message news:DR*****************@newsread2.news.pas.earthl ink.net

This is not your problem, however. Nor is it multiple inclusion.
Include guards protect against the same thing appearing multiple
times in a single translation unit, but not against the same thing
appearing in two different translation units. For example, if I have
a main() function in a.cpp and I also have a main() function in
b.cpp, my program won't link. This is basically what you are doing -
you have 'msg' appearing in both .cpp files, because each has it's
own #included copy of globals.h.

The proper way to do this is to only declare 'msg' in the header (with
'extern', and no initialization) and then have its definition (along
with the initialization) in a .cpp file somewhere.


I agree that that is the best way to do it, but I think that the strategy of
the OP should still work. This is because const variables should have
internal linkage by default, i.e.,

const char *msg = "Wiggety wack";

should be equivalent to

static const char *msg = "Wiggety wack";

The latter will certainly build without a problem and so should the former.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #4
"John Carson" <do***********@datafast.net.au> wrote in message:
This is not your problem, however. Nor is it multiple inclusion.
Include guards protect against the same thing appearing multiple
times in a single translation unit, but not against the same thing
appearing in two different translation units. For example, if I have a main() function in a.cpp and I also have a main() function in
b.cpp, my program won't link. This is basically what you are doing - you have 'msg' appearing in both .cpp files, because each has it's
own #included copy of globals.h.

The proper way to do this is to only declare 'msg' in the header (with 'extern', and no initialization) and then have its definition (along with the initialization) in a .cpp file somewhere.
I agree that that is the best way to do it, but I think that the

strategy of the OP should still work. This is because const variables should have internal linkage by default, i.e.,

const char *msg = "Wiggety wack";

should be equivalent to

static const char *msg = "Wiggety wack";

The latter will certainly build without a problem and so should the former.


The former does not build on VC7.1 or GCC 3.2, and I don't see why it
should. 3.5/3 says names explicitly declared const have internal
linkage if they are objects or references; msg is a pointer. Could you
explain why you think msg should have internal linkage?

Regards,
Jonathan
Jul 22 '05 #5
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de
"John Carson" <do***********@datafast.net.au> wrote in message:

I agree that that is the best way to do it, but I think that the
strategy of the OP should still work. This is because const
variables should have internal linkage by default, i.e.,

const char *msg = "Wiggety wack";

should be equivalent to

static const char *msg = "Wiggety wack";

The latter will certainly build without a problem and so should the
former.


The former does not build on VC7.1 or GCC 3.2, and I don't see why it
should. 3.5/3 says names explicitly declared const have internal
linkage if they are objects or references; msg is a pointer. Could you
explain why you think msg should have internal linkage?

Regards,
Jonathan

Actually, we are both wrong. A const pointer does have internal linkage, as
I suggested, but

const char *msg = "Wiggety wack";

does not define a const pointer. Rather, it defines a non-const pointer to
const char. To create a const pointer, we need:

const char * const msg = "Wiggety wack";

This compiles on VC++ 7.0.

As for the standard, it says:

"A name having namespace scope (3.3.5) has internal linkage if it is the
name of
— an object, reference, function or function template that is explicitly
declared static or,
— an object or reference that is explicitly declared const and neither
explicitly declared extern nor previously declared to have external linkage;
or ..."

Observe that there is no explicit mention of pointers under the first dashed
point dealing with the use of the static keyword, yet we know that declaring
a pointer static will give it internal linkage. Accordingly, I infer that
pointers are included as "objects" under both dashed points.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6
"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message


The former does not build on VC7.1 or GCC 3.2, and I don't see why it should. 3.5/3 says names explicitly declared const have internal
linkage if they are objects or references; msg is a pointer. Could you explain why you think msg should have internal linkage?

Regards,
Jonathan

Actually, we are both wrong. A const pointer does have internal

linkage, as I suggested, but

const char *msg = "Wiggety wack";
Your right. Duh!

As for the standard, it says:

"A name having namespace scope (3.3.5) has internal linkage if it is the name of
— an object, reference, function or function template that is explicitly declared static or,
— an object or reference that is explicitly declared const and neither explicitly declared extern nor previously declared to have external linkage; or ..."

Observe that there is no explicit mention of pointers under the first dashed point dealing with the use of the static keyword, yet we know that declaring a pointer static will give it internal linkage. Accordingly, I infer that pointers are included as "objects" under both dashed points.


I don't follow your reasoning here. The passage is talking about
const, not static. I believe objects and pointers are usually treated
separately by the standard.

Jonathan
Jul 22 '05 #7

"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
"A name having namespace scope (3.3.5) has internal linkage if it is the name of
— an object, reference, function or function template that is explicitly declared static or,
— an object or reference that is explicitly declared const and neither explicitly declared extern nor previously declared to have external linkage; or ..."


I guess the answer comes from 1.8/1.

Jonathan
Jul 22 '05 #8
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de
"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...

"A name having namespace scope (3.3.5) has internal linkage if it
is the name of
— an object, reference, function or function template that is
explicitly declared static or,
— an object or reference that is explicitly declared const and
neither explicitly declared extern nor previously declared to have
external linkage; or ..."


I guess the answer comes from 1.8/1.

Jonathan

Yep. "An object is a region of storage."
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #9
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de
"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message

>

The former does not build on VC7.1 or GCC 3.2, and I don't see
why it should. 3.5/3 says names explicitly declared const have
internal linkage if they are objects or references; msg is a
pointer. Could you explain why you think msg should have internal
linkage?

Regards,
Jonathan

Actually, we are both wrong. A const pointer does have internal
linkage, as I suggested, but

const char *msg = "Wiggety wack";


Your right. Duh!

As for the standard, it says:

"A name having namespace scope (3.3.5) has internal linkage if it
is the name of
— an object, reference, function or function template that is
explicitly declared static or,
— an object or reference that is explicitly declared const and
neither explicitly declared extern nor previously declared to have
external linkage; or ..."

Observe that there is no explicit mention of pointers under the
first dashed point dealing with the use of the static keyword, yet
we know that declaring a pointer static will give it internal
linkage. Accordingly, I infer that pointers are included as
"objects" under both dashed points.


I don't follow your reasoning here. The passage is talking about
const, not static. I believe objects and pointers are usually treated
separately by the standard.


Look again. The first dashed point says:

"an object, reference, function or function template that is explicitly
declared static"
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #10

"John Carson" <do***********@datafast.net.au> wrote in message
news:40********@usenet.per.paradox.net.au...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message

Observe that there is no explicit mention of pointers under the
first dashed point dealing with the use of the static keyword, yet we know that declaring a pointer static will give it internal
linkage. Accordingly, I infer that pointers are included as
"objects" under both dashed points.


I don't follow your reasoning here. The passage is talking about
const, not static. I believe objects and pointers are usually treated separately by the standard.


Look again. The first dashed point says:

"an object, reference, function or function template that is

explicitly declared static"


I see what your argument was now:

1. We've all heard that delaring a pointer static has gives it
internal linkage.
2. This must be the passage which states that rule.
3. Therefore, this passage must be refering to pointers (among
other things)

Right? :-)

This get's the interpretive porcess exactly backwards. I'd like to
think we should start with the text, read it carefully, and figure out
what it means. Of course, in this case, I did a horrible job.

Jonathan


Jul 22 '05 #11
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bu************@ID-216073.news.uni-berlin.de

I see what your argument was now:

1. We've all heard that delaring a pointer static has gives it
internal linkage.
2. This must be the passage which states that rule.
3. Therefore, this passage must be refering to pointers (among
other things)

Right? :-)
Exactly.
This get's the interpretive porcess exactly backwards. I'd like to
think we should start with the text, read it carefully, and figure out
what it means.


Yes, that would be ideal. But the standard often isn't that easy to
interpret. To be a really good interpreter of the standard, I think you
would have to read the whole thing from the beginning --- which I am
disinclined to do.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #12
Fritz Foetzl wrote:

[snip]

This won't link.
Try this:
cat globals.h #ifndef GUARD_GLOBALS_H
#define GUARD_GLOBALS_H 1

const
char *const msg = "Wiggety wack";
extern void getWiggety(void);

#endif//GUARD_GLOBALS_H
cat functions.cpp #include <iostream>
#include "globals.h"

void getWiggety(void) {
std::cout << msg << std::endl;
}
cat driver.cpp #include <iostream>
#include "globals.h"

int main(int argc, char* argv[]) {
std::cout << msg << std::endl;
getWiggety();
return 0;
}
g++ -Wall -ansi -pedantic -o driver driver.cpp functions.cpp
./driver

Wiggety wack
Wiggety wack

Jul 22 '05 #13
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<bu************@ID-216073.news.uni-berlin.de>...
I think your problem has nothing to do with include guards. The header
"globals.h" is properly included twice, once when each of the files
"driver.cpp" and "functions.cpp" is compiled. The problem is that msg
is being defined twice in the same program, violating the ODR.

You should probably decalre it extern and define it in "global.cpp",
or use an inline function instead


Maybe it works differently with gcc, or maybe I've just never
adequately understood the problem. Declaring my globals extern in the
header and defining them in a seperate .cpp file works beautifully.

I'm over that hurdle. Thanks for your help!

ff
Jul 22 '05 #14

"Fritz Foetzl" <fr**********@hotmail.com> wrote in message
news:d2**************************@posting.google.c om...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<bu************@ID-216073.news.uni-berlin.de>...
I think your problem has nothing to do with include guards. The header "globals.h" is properly included twice, once when each of the files "driver.cpp" and "functions.cpp" is compiled. The problem is that msg is being defined twice in the same program, violating the ODR.

You should probably decalre it extern and define it in "global.cpp", or use an inline function instead
Maybe it works differently with gcc, or maybe I've just never
adequately understood the problem. Declaring my globals extern in

the header and defining them in a seperate .cpp file works beautifully.

I'm over that hurdle. Thanks for your help!

ff


Glad to help. Now if I could just erase my others posts in this
thread, in which I misread the standard half a dozen times, I'd be
happy forever!

Jonathan
Jul 22 '05 #15

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

Similar topics

2
by: Jochen Zeischka | last post by:
Hi everybody! I have a question concerning code organisation. Suppose I have the following header file: #ifndef SOME_NAME #define SOME_NAME namespace N { void F()
2
by: Martin Magnusson | last post by:
I have a problem with multiple definitions that I can't quite straighten out. I have a templated class defined inside a namespace, and I want to create a function in that namespace that works on...
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...
5
by: Dave | last post by:
Hello all, To protect against multiple inclusions, it is standard practice to enclose the contents of a header file in a construct like this: #ifndef FOO_INCLUDED #define FOO_INCLUDED .......
14
by: Carramba | last post by:
hi! I have program with several funktion witch are in separete files, I have one include file were I have definet some variables and initiated 'const double fVar=0.874532;' this files is includet...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
6
by: vsgdp | last post by:
I was looking at some library code today and noticed something like this: // sublibrary.h // define some constants, enums, symbols #include "componentA.h" #include "componentB.h" #include...
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
9
by: ramsatishv | last post by:
Hi, If I include a ".h" file for multiple times, will it increase my program size?? Regards Ram.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.