Internal linkage and static variable

Hi guys!

I just learn about static and extern keywords. Everything make a sense for me BUT there is one thing which doesn't. Most of articles, forums etc wrote that the static keyword change linkage to internal(the static variable or function have only file scope - not visible for other files more precisely compilation unit). I do a test: I have a header file with static int variable. When I include this header in my main.cpp I see this variable so whats going on? So pls guys help me to understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  //header.h
  
  static int var; // internal linkage, visible only in this file scope


  //main.cpp

  #include "header.h"

  int main (){
       int i = var; // var is visible here even if it static
       std::cout << i << std::endl;
}

 
Last edited on
Each compilation unit gets its own copy of the variable.

header.h
1
2
3
4
static int var;

void set_other_var(int);
int get_other_var();

other.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "header.h"

void set_other_var(int value)
{
	var = value;
}

int get_other_var()
{
	return var;
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "header.h"
#include <iostream>

int main ()
{
	var = 12;
	set_other_var(88);

	std::cout << "main var:  " << var << std::endl;
	std::cout << "other var: " << get_other_var() << std::endl;
}

output
main var:  12
other var: 88
Last edited on
Hi. Ok thanks for quick replay. BUT there is another question for you:

You said every compilation unit has its own copy of var but when you change your main.cpp to folowing:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "header.h"
#include <iostream>

var = 12; // now I make definition of var here = ERROR REDEFINITION

int main ()
{
	// var = 12; used to be here
	set_other_var(88);

	std::cout << "main var:  " << var << std::endl;
	std::cout << "other var: " << get_other_var() << std::endl;
}


now I get redefinition error even if, static int var has file scope. How is it possible that in the block of the function is work but outside it doesn't. Can you explain ?
You can never assign to variables outside functions like that so I assume you mean line 4 should be static int var = 12;.

"file scope" is a bit of a misnomer because the scope extends until the end of the whole translation unit.

A translation unit is the content of a source file + the content of all the headers that it includes. Each translation unit is compiled in separation without knowledge of other translation units. When all translation units has been compiled they can be linked together to produce the executable. That is why if var had been defined without static in my code (giving it external linkage) you would still been able to compile main.cpp and other.cpp, but the linking would fail because var has been defined in multiple translation units.
Last edited on
Ok. And if you want to have a var as global variable ? I mean ok, maybe its a dumb question. You said I can never assign to variables outside function but there are global variables which works like that . What if I want to have static global variable. Pls expend that part of why I cant never assign to variables outside especially if is it static.


And the second thing just make it simple

This is only declaration right ?
1
2
3
 // header.h

static int var; 


and watch this
1
2
3
4
5
6
7
// main.cpp

var = 12; // get redefinition error , but it was not defined at all yet ..so what i MISS OMG PLS TELL ME !!

int main(){
....
}
Last edited on
You can always initialize the variable when you define it but if you want to later assign another value to it you would have to do that inside a function.

Static global variables are mostly used for constants (const have internal linkage by default). You cannot modify a constant so you normally not notice that each translation unit has its own copy.

Mutable global variables are usually considered bad practice, but if you insist you can declare them with the extern keyword in the header and then define them as normal inside a source file. This ensures the variables are only defined in one translation unit.

header file
 
extern int var;

source file
 
int var = 12;
Tony321 wrote:
This is only declaration right ?
1
2
 // header.h
static int var; 

No it's a definition, unless it's a class member in which case static has a totally different meaning.

You can declare global variables with the extern keyword but that doesn't work with static.
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/208987/
Topic archived. No new replies allowed.