F
Fritz Foetzl
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
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