473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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..#defin e..#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..#defin e..#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
14 1771

"John Carson" <do***********@ datafast.net.au > wrote in message
news:40******** @usenet.per.par adox.net.au...
"Jonathan Turkanis" <te******@kanga roologic.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******@kanga roologic.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******@kanga roologic.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**********@h otmail.com> wrote in message
news:d2******** *************** ***@posting.goo gle.com...
"Jonathan Turkanis" <te******@kanga roologic.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
21078
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
1769
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 a specific instance of the templated class. Like this: // file Numerical.hpp:
6
1751
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 dependencies of these dependencies and so on. This is quite ugly. A start of an "Example.cpp" file could look like this
5
3726
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 .... #endif
14
2549
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 in all other files containing funktions, when I compile I get this error multiple definition of `fVar' why id that? I have only defined it one in include file? --
6
5205
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
2609
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 "componentC.h"
6
3907
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: // SomeClass.hh #ifndef SOME_CLASS_HH #define SOME_CLASS_HH class SomeClass {
9
1727
by: ramsatishv | last post by:
Hi, If I include a ".h" file for multiple times, will it increase my program size?? Regards Ram.
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.