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 14 1747
"Fritz Foetzl" <fr**********@h otmail.com> wrote in message
news:d2******** *************** ***@posting.goo gle.com... 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
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
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..#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_
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.
"Kevin Goodsell" <us************ *********@never box.com> wrote in
message news:DR******** *********@newsr ead2.news.pas.e arthlink.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)
"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
"Jonathan Turkanis" <te******@kanga roologic.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)
"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
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
"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
"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
"Jonathan Turkanis" <te******@kanga roologic.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.par adox.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)
"Jonathan Turkanis" <te******@kanga roologic.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.par adox.net.au... "Jonathan Turkanis" <te******@kanga roologic.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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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()
|
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:
|
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
|
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
|
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...
| |
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 !!
|
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"
|
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
{
|
by: ramsatishv |
last post by:
Hi,
If I include a ".h" file for multiple times, will it increase my
program size??
Regards
Ram.
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |