I have a global variable in one of the cpp files, where I am assigning a value to it. Now in order to be able to use it in another cpp file, I am declaring it as extern
and this file has multiple functions that use it so I am doing this globally. Now the value of this variable can be accessed in one of the functions and not in the other one. Any suggestions except using it in a header file would be good because I wasted 4 days playing with that.
-
3This one is nearly impossible to answer without a magic guessing hat, or seeing some code. – Sergey Kalinichenko Sep 5 '12 at 22:06
-
What are the symptoms? Compilation failure, crash when running? – Daniel Fischer Sep 5 '12 at 22:12
-
No , when I write out that variable to a file ( from within the function where I cannot access it ) it just prints null, i.e nothing – Venushree Patel Sep 5 '12 at 22:15
-
Possible duplicate of Accessing a global variable defined in a .cpp file in another .cpp file – CinCout Aug 22 '17 at 4:44
Add a comment
|
Sorry, I'm ignoring the request for answers suggesting anything other than the use of header files. This is what headers are for, when you use them correctly... Read carefully:
global.h
#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H
// This is a declaration of your variable, which tells the linker this value
// is found elsewhere. Anyone who wishes to use it must include global.h,
// either directly or indirectly.
extern int myglobalint;
#endif
global.cpp
#include "global.h"
// This is the definition of your variable. It can only happen in one place.
// You must include global.h so that the compiler matches it to the correct
// one, and doesn't implicitly convert it to static.
int myglobalint = 0;
user.cpp
// Anyone who uses the global value must include the appropriate header.
#include "global.h"
void SomeFunction()
{
// Now you can access the variable.
int temp = myglobalint;
}
Now, when you compile and link your project, you must:
- Compile each source (.cpp) file into an object file;
- Link all object files to create your executable / library / whatever.
Using the syntax I have given above, you should have neither compile nor link errors.
-
2+1: Thanks — I was going to start an answer with 'the only correct way to deal with this is by using header files properly'. You've saved me the effort. The only comment I'd make is that in
global.cpp
, I'd probably put an explicit initializer (even if it is the default of 0), just to 'make sure'. I'll also mention that What areextern
variables in C applies to C++ with very few if any changes. – Jonathan Leffler Sep 5 '12 at 22:26 -
Cheers. I was trying to keep it bare-bones and left that out, but an initializer is probably a good idea. – paddy Sep 5 '12 at 22:31
-
1A little late... But you can compile and link at the same time by running
gcc file1.c file2.c -o myprogram
... – NerdOfCode Mar 18 '18 at 5:02 -
@NerdOfCode The
g++
and thegcc
do not exactly behave the same in terms of inter-linkage such as the need ofextern "C"
and name mangling i would be careful and emphasize this very important difference – eigenfield Mar 15 '20 at 19:12 -
great answer! But a question, what if my variable is an object, for example a vector: what would be a good way of initializing it (is not enough with constructors)? Perhaps making a function in global.cpp that makes the initialization and from user.cpp calling that function before using the variables? – Eduardo Pascual Aseff Mar 27 '20 at 16:32