trying to access a variable in a header file

This is what I want to accomplish:
Header file - abc.h
#ifndef ABC_H
#define ABC_H
extern bool myBool;
#endif

CPP file1 - abc1.cpp
#include "abc.h"
if (somecondition == NULL)
{
myBool = false;
}

CPP file2 - abc2.cpp
#include "abc.h"
if (myBool = false)
{
some actions;
}

During compilation - I am getting:
abc1.obj : error LNK2001: unresolved external symbol "bool myBool"
abc1.exe : fatal error LNK1120: 1 unresolved externals

How do I accomplish this ?
Thanks in Advance

You don't need the extern in the header file.

-Albatross
If I don't put the extern in the header file then, I get a bunch of these errors:
abc1.obj : Error LNK2005: "bool myBool" already defined in abcN.obj
abc2.obj : ......same.....
abc1.exe : fatal error LNK1169: one or more multiply defined symbols found

Okay... I will need the whole code for each file, if you would, please. It's possible the error is referring to something else.

-Albatross
What command line is being used for compiling?
Last edited on
These are very large files....and a large number of files as well. I am using VS2005.
To make it slightly more elaborate:
Constants Header File 1 : ABC1.h
#ifndef ABC1_H
#define ABC1_H

#define CONST_A "aa_ab"
......bunch of constant definitions
bool myBool;
#endif

Header File 2: ABC2.h
#ifndef ABC2_H
#define ABC2_H

#include "ABC1.h"
#include <iostream>
....bunch of include files
....bunch of function protoypes
#endif

Header File 3: ABC3.h
#ifndef ABC3_H
#define ABC3_H

#include "ABC1.h"
#include "ABC2.h"
#include <fstream>
....bunch of include files

class ABC3
{
int var1;
int var2;
std::map....
std:: ofstream.....
public:
ABC3(std::map..., std::ofstream....)
int startabc3func1();
int func2();
};
#endif

CPP file 1 - MAIN FILE - references/calls all other files : ABC2.cpp
#include <...>
#include "ABC2.h"
using namespace std;

extern int main(int argc, char *argv[])
{
char *varA1 = NULL;
..bunch of variable definitions
if (varA1 = NULL)
{
myBool = false
}
....
ABC3.startabc3func1();
return...
}

CPP file 2: ABC3.cpp
#include <...>
#include "ABC3.h"

using namespace std;

ABC3::ABC3(map....)
{
}

int ABC3::startabc3func1()
{
....
if (myBool = false)
{
}
else
{
}

....
return;
}













...okay. Okay. What the heck is going on here?!?

First, please use code tags.

Second, why does your int main() have an extern preceding it?

Third, eliminate every instance of extern in your program. You don't need it unless you decide to omit the header files.
extern is used when you want to pass a note to the compiler and later linker that something is declared and/or defined in another C++ file and later object file.

Fourth, you do realize that #include "ABC1.h" is redundant when you have #include "ABC2.h"?

There's probably something in Fifth,, but I haven't found it yet.

-Albatross


Last edited on
I guess I am not familiar with code tags ....is it code ..... /code ?
Let me try it out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//-----Constants Header File 1 : ABC1.h
#ifndef ABC1_H
#define ABC1_H

#define CONST_A "aa_ab"
//......bunch of constant definitions
bool myBool;
#endif

//----Header File 2: ABC2.h
#ifndef ABC2_H
#define ABC2_H

#include "ABC1.h"
#include <iostream>
//....bunch of include files
//....bunch of function protoypes
#endif

//-----Header File 3: ABC3.h
#ifndef ABC3_H
#define ABC3_H

#include "ABC1.h"
#include "ABC2.h"
#include <fstream>
//....bunch of include files

class ABC3
{
int var1;
int var2;
std::map....
std:: ofstream.....
public:
ABC3(std::map..., std::ofstream....)
int startabc3func1();
int func2();
};
#endif

//------CPP file 1 - MAIN FILE - references/calls all other files : ABC2.cpp
#include <...>
#include "ABC2.h"
using namespace std;

extern int main(int argc, char *argv[])
{
char *varA1 = NULL;
..bunch of variable definitions
if (varA1 = NULL)
{
myBool = false
}
....
ABC3.startabc3func1();
return...
}

//------CPP file 2: ABC3.cpp
#include <...>
#include "ABC3.h"

using namespace std;

ABC3::ABC3(map....)
{
}

int ABC3::startabc3func1()
{
....
if (myBool = false)
{
}
else
{
}

....
return;
}


The code was working absolutely fine except when I wanted to add this extra piece of logic with bool myBool
Yay, you got the code tags working!

I recommend having myBool as a part of your class. I'm not 100% sure what's going on, but placing yourBool there might solve it.

I still don't know why you have extern int main(args).

-Albatross
This is one of the reasons why global variables are bad. Declaring it in a header file and including that in multilple source files results in the error that you're getting about the multiple definitions.

I think this is a good time to recommend the Singleton Pattern1. Here's a quick, untested, mock-up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Global.hpp
#ifndef GLOBAL_HPP
#define GLOBAL_HPP

class Global {

    bool myBool_;

    static Global * instance_;

    Global() : myBool( false ) {}
    Global & operator=( const Global & );

public:

    bool getMyBool() const { return myBool_; }
    void setMyBool( bool b ) { myBool = b; }

    static Global * getInstance() {
        if( !instance_ ) instance_ = new Global();
        return instance_;
    }
};

#endif 


1
2
3
4
5
// Global.cpp
#include "Global.hpp"

// static member initialization
Global * Global::instance_ = 0;


1
2
3
4
5
6
// other file(s)

#include <iostream>
#include "Global.hpp"
//...
std::cout << Global::getInstance()->getMyBool();



1 Note that this approach is not ideal, either. It would be best to determine "who" owns myBool and try to avoid all of this just to support a global variable.
Last edited on
I guess 2 questions:
1. If I include myBool as a part of the class - would multiple source files be able to access this flag ? One source file sets the flag based on a certain condition and the second source file executes a logic based on what the flag is set to ?

2. What is a singleton ?

Thanks...but I'm relatively new to this.

Can you elaborate on the - "who" owns myBool ?

The Singleton example helps - but as you say - all of this to support a global variable !!!
After thinking about why the example above should work, I'm starting to suspect that if you have an extern declaration in a header file and a single definition in a source file that the symbol would still obey the one definition rule and be accessible... All you need is external linkage for myBool. I beleive that the extern keyword is telling the compiler that treat that line as a declaration only--meaning that you would need a separate definition. Did you try adding bool myBool; in the cpp file with everything else like the first post?

Last edited on
With
extern bool myBool;

in the main constants header file

With
bool myBool;

in both source files

It compiles and links fine without errors
I don't think that's quite what you're after. I think you want it to have its own source file and just include the header file in the other two source files.
Last edited on
Allright...there are a couple of good tips here. Let me try these out and get back.


Thanks a ton.

Topic archived. No new replies allowed.