473,698 Members | 2,300 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 1759

"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
21067
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
1764
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
1748
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
3721
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
2545
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
5202
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
2605
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
3905
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
1725
by: ramsatishv | last post by:
Hi, If I include a ".h" file for multiple times, will it increase my program size?? Regards Ram.
0
8676
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5860
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.