How to use global variables in inline functions ?

Hi all,

I have declared global variables in the main file. Then in other files where I need it, I refer to these variables with extern in the .cpp.

But I also need to refer to some of these global variables in some inline functions, and thus to refer to these variables with extern in the .h. But with #include directives, it looks like a big mess. Besides, some types are not yet defined.

So my question is : what is the best method or paradigm to use global variables in inline functions ?
what is the best method or paradigm to use global variables in inline functions ?


The best method or paradigm is to not use global variables.
Thanks Moschops. But I would like to avoid having to rewrite all this code. I miss inline function body shall be in header files. My opinion is that it is a bad design of the c++ compiler..... To what you will repeat that the bad design is to use global variables.

You do tell me what not to do, but not what to do. Shall I put everything in classes and use friend ? What is the good way ?
I don't see how using global variables in inline functions is different from other functions.
Thanks, Peter87. I was scratching my head thinking the exact same thing for a second there.

To the OP: Moschops is right. You may want to avoid rewriting the code but the sooner you do so, the less chance you've got of being stung by it.
Thanks all,

I don't see how using global variables in inline functions is different from other functions.

That's due to my lack of experience in identifying the roots of the problems.

I found answers here : http://www.cplusplus.com/forum/articles/10627/2/

I will let you know if I could manage it with that.
btw, why global variables is bad? could someone give me a link for the explanation?
Last edited on
Somthing strange I don't understand. Here is my code :

Global.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef GLOBAL_H_
#define GLOBAL_H_

#include <string>

#ifndef GLOBAL // cf http://www.cplusplus.com/forum/articles/10627/2/

//enum TOKEN { AAA, BBB, CCC, DDD};
#define AAA 0
#define BBB 1
#define CCC 2
#define DDD 3
extern const std::string token[];

#endif

#endif /* GLOBAL_H_ */ 


Global.cpp :
1
2
3
4
5
6
7
#define GLOBAL // cf http://www.cplusplus.com/forum/articles/10627/2/
#include "Global.h"

using namespace std;

//enum TOKEN             { AAA, BBB, CCC, DDD};
const string token[] = {"AAA", "BBB", "CCC", "DDD"};


Test.cpp
1
2
3
4
5
6
7
8
9
#include "Global.h"
#include "test.h"

using namespace std;

int main(int argc, const char *argv[]) {
    cout << token[BBB] << endl;
    return 0;
}


Test.h
1
2
3
4
5
6
7
#ifndef TEST_H_
#define TEST_H_

#include <iostream>
#include <string>
#include <cstdlib>
#endif /* TEST_H_ */ 


I have a linker error :
Building target: test
Invoking: GCC C++ Linker
g++ -L/usr/lib64 -o"test"  ./src/Global.o ./src/test.o 
./src/test.o: In function `main':
test.cpp:(.text+0x1b58): undefined reference to `token'
collect2: ld has returned 1 code running status
make: *** [test] Error 1


If I comment : #ifndef GLOBAL in Global.h, then it works.

Could someone explain that to me please ? Why does it fail with it ?



Last edited on
Const variables have internal linkage. So when you exclude the declaration

extern const std::string token[];

the compiler considers the const array of strings as internal to the module where it is declared.
Thanks vlad. Wouldn't have been better for the compiler to have a homogeneous approach.
Topic archived. No new replies allowed.