24

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.

4
  • 4
    This one is nearly impossible to answer without a magic guessing hat, or seeing some code. Commented Sep 5, 2012 at 22:06
  • What are the symptoms? Compilation failure, crash when running? Commented Sep 5, 2012 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 Commented Sep 5, 2012 at 22:15
  • Possible duplicate of Accessing a global variable defined in a .cpp file in another .cpp file Commented Aug 22, 2017 at 4:44

1 Answer 1

62

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:

  1. Compile each source (.cpp) file into an object file;
  2. 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.

Sign up to request clarification or add additional context in comments.

7 Comments

+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 are extern variables in C applies to C++ with very few if any changes.
Cheers. I was trying to keep it bare-bones and left that out, but an initializer is probably a good idea.
A little late... But you can compile and link at the same time by running gcc file1.c file2.c -o myprogram...
@NerdOfCode The g++ and the gcc do not exactly behave the same in terms of inter-linkage such as the need of extern "C" and name mangling i would be careful and emphasize this very important difference
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.