0

In C++, I have been taught that a static linkage global variable is created when program starts and destroyed in the end of program. If the variable get destroyed in the end of the program (not file), I think there's definitely a way to use it in other files. I want to know how.

Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
  • 1
    ~Duplicate of http://stackoverflow.com/questions/2841762/why-wont-extern-link-to-a-static-variable, which also gives good, general advice on scoping. – Jonathon Reinhart Jul 22 '15 at 11:11
  • That's totally a different question. I m asking for a trick (if exists) to make my static variable accesible in my related project files. –  Jul 22 '15 at 12:12
  • The answer is a resounding, unabated, ***NO***. You **cannot** access `static` variables outside of the file where they are defined. Full stop. That is not a "totally" different question. The OP tried doing what you're asking and wondered why it didn't work. Because *it's intentionally not supposed to work*. – Jonathon Reinhart Jul 22 '15 at 12:16
  • Yeah...answers are relative. But question is asking something else. Mine can't be called a duplicate one. –  Jul 22 '15 at 12:41
  • You're mixing up two different things. A **global** variable *will* be created when the program starts up. A **static** variable cannot be accessed from outside its scope. But aside from global variables being frowned upon on principles, the *order* of initialization is not specified, so you might set yourself up for some trouble. Bottom line, yes you can do this, no you should not do this. – DevSolar Jul 22 '15 at 15:34

2 Answers2

2

There are multiple meanings to static.

A variable declared at the file scope with static is visible only to functions in that file. You cannot use a static variable defined in one file from another file.

It sounds like you want a normal global variable. Just leave off the static.

"Local" variables, declared at at the function scope, have a default "auto" lifetime - their values persist only as long as the function executes, and once the functin returns, the value is gone. You can change this to live as long as the program with static.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • @ Jonathon Reinhart So what's the other meaning of that. Leaving the static keyword makes it a external variable and a non-const external variable is considered dangerous. I want to know if I can make a static variable accessible across multiple files. –  Jul 22 '15 at 10:42
  • You *are* asking for the "dangerous" (more like not-recommended) behavior. – Jonathon Reinhart Jul 22 '15 at 10:51
2

If the variable is defined in a header, simply include the header and use it. If it's declared globally in a compilation module (i.e. .cpp file), then declare an extern version of it and use it. Note, this is not static which implies internal linkage which explicitly reduces the scope of the variable to a single compilation unit. This is global / external linkage.

E.g.

module1.cpp

int globalX = 5;

module2.h

extern int globalX;

module2.cpp

std::cout << globalX;

Been a while since I've done much C++, but I believe this should work.

Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67
  • Please. Code this up and compile it. `extern static` makes no sense. There's no such thing as a "static global". `static` ***explicitly*** means "not visible to other compilation units! See http://stackoverflow.com/questions/2841762/why-wont-extern-link-to-a-static-variable if you refuse to believe me. – Jonathon Reinhart Jul 22 '15 at 11:29
  • @SandyChapman All you did was remove the `static` keyword. Your answer still reads `"If it's declared globally static in a compilation module (i.e. .cpp file), then declare an extern version of it and use it."` which is 100% incorrect. You also still have the misleading `static` in the name of the variable! You're yanking everyone's chain right now. – Jonathon Reinhart Jul 22 '15 at 11:32
  • I understand that people get answers wrong - it happens to everyone occasionally. But it is *irresponsible* to up-vote and accept incorrect answers, especially when it is so trivial to test. The responsible thing to do on a highly-respected site like Stack Overflow is to accept when you're wrong and remove your answer. (I've done it before.) – Jonathon Reinhart Jul 22 '15 at 11:33
  • I've gone ahead and adjusted the answer to hopefully be factually correct even if it doesn't accurately answer the question. – Sandy Chapman Jul 22 '15 at 11:36
  • @Jonathon Reinhart I only accepted that answer because the header file trick will work. I have questioned the example code (where extern is used with static) in comment. –  Jul 22 '15 at 12:09
  • @Sandy That's it...using a header will get me my job done. Thnx Sandy. Ammmm...ur module2.h example is a bit confusing. You are using the extern keyword with a static variable. Would it work? As Jonathan said it won't...you should update ur answer. –  Jul 22 '15 at 12:17
  • 1
    @DevashishJaiswal Using a header file is not a "trick". If you put `static int x` in a header file, *you're doing it wrong*. That will cause ***multiple unrelated*** variables, all named `x` to exist; one in each `cpp` file that includes said header file. – Jonathon Reinhart Jul 22 '15 at 12:18
  • @jonthanIf I define a static variable in a header file...the including .cpp file would be able to access that variable and variable still has a file scope. If a local variable with same name is defined inside any function in the same file, I can use the scope operator to tell the compiler which version of the identifier I want to use. If the same header file gets included in other .cpp files, that file will use a completely different variable that also has a file scope. That's what I think. Or will that give an error? If yes. Plz explain why? –  Jul 22 '15 at 12:30