1

I have a global variable in.h file

extern char Name[10];

I want to initialize this variable in another file profile.cpp and later use in other files a.cpp,b.cpp..

profile.cpp

char Name[10]="John";

a.cpp

if(id==10)
{
cout<<Name;
}

How to use the same variable in a.cpp with its value assigned in profile.cpp? Should it be delared as a struct and access in multiple files? Can someone show a breif description of how to use it?

user2333234
  • 553
  • 1
  • 4
  • 7

6 Answers6

1

I agree with the comments here advising against the use of global variables, they can cause problems related to initialization order, they can create problems with thread safety, they pollute the namespace, etc.

The idiomatic way of dealing with globals in c++ is to put them in a struct (as you alluded to) or to put them in a namespace. You can read more about that here. However, if you must use globals (in c++ you would say variables and functions at global scope) here's a simple example patterned after your code:

makefile

main: main.cpp a.cpp profile.cpp my_globals.h
    g++ -O3 -o main main.cpp a.cpp profile.cpp

clean:
    $(RM) main

my_globals.h

// This is a header file and should only contain declarations
extern char Name[10];         // declaration of Name variable
extern void use_global(int);  // declaration of use_global function

main.cpp

#include "my_globals.h"
int main(int argc, char * argv[])
{
    use_global(10);           // use of use_global function
    return 0;
}

a.cpp

#include "my_globals.h"
#include <iostream>
using std::cout;
using std::endl;

void use_global(int id) {       // definition of use_global function
    if(id==10)
    {
        cout<<Name<<endl;       // use of Name global variable
    }
}

profile.cpp

#include "my_globals.h"
char Name[10]="John";           // definition of Name variable

You build it

$ make
g++ -O3 -o main main.cpp a.cpp profile.cpp
$ 

and you run it:

$ ./main
John
$ 
Community
  • 1
  • 1
amdn
  • 11,314
  • 33
  • 45
0

Your a.cpp should include the .h file with the declaration of the variable:

#include "a.h"

// ...
if (id == 10)
    cout << Name;

That's pretty much all you need to do (at least until you realize that using a global was a mistake, and you want to overhaul the code to get rid of it).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Are you having it in .h file as well declare in profile.cpp and initialize? Did it work.

You should remove it from .h since you are declaring and initializing in profile.cpp, the variable 'Name' should be declared as extern in files that use them;

Prabhu
  • 3,443
  • 15
  • 26
0

If your variable must be read only from some files, the best way to do this is make a getter function, so you can ensure that the value of your variable cannot be changed in any module:

// global header file which declares the global getter function 
#include <string>

std::string Name();

Variable declaration in the profile file, the static keyword hides your variable, so other files can only access to it using the getter Name():

// profile.cpp file

#include <string>

static std::string Name = "John";

File a.cpp tha use Name

#include "global.h"

// ...
if (id == 10)
    cout << Name();

If you need to change value you can also implement a setter function.

AngeloDM
  • 397
  • 1
  • 8
0

The best thing to do is to use a singleton. It is a style which is generally preferred to global variables and, more importantly, it allows you to get rid of all sort of problems derived from the undefined order of initialization of global variables.

You can read a lot on singletons on Andrei Alexandrescu's book Modern C++. You can implement singleton using the Loki library (by Alexandrescu himself).

Stefano Falasca
  • 8,837
  • 2
  • 18
  • 24
0

Include the header file which contains the global variable in files that are going to use it.

For future reference you can't call a variable unless it either declared or its in a header file the file loads.