Why is the function in the header file doesn't see global variable?

Read the book of Stroustrup and zastupilsya on one task. More to the point. There are three files: my.h; my.cpp; use.cpp
The contents of my.h:
extern int foo;
void print_foo();
void print(int);

Content my.cpp:
#include <iostream>
#include "my.h"

using namespace std;

void print_foo(){
 cout << "foo:" << foo << endl;
return;
}
void print(int i){
 cout << "i:" << i << endl;
return;
}</iostream>

Content use.cpp:
#include <iostream>
#include "my.h"

using namespace std;

int main(int argc, char *argv[])
{
 int foo = 7;
print_foo();
print(99);
 return 0;
}</iostream>


So here's the question. Why is the function print_foo (in the definition file) does not see the variable that I described in main()? And how to fix it?
July 4th 19 at 23:06
2 answers
July 4th 19 at 23:08
Solution
All that is described inside the block (inside the curly brackets) visible only within that block. This is the answer to your "why".

The use of global entities negatively affects the quality of the code. Some industry development standards expressly prohibit global variables.

To work, you have int foo = 7 to make beyond the main.
July 4th 19 at 23:10
Because foo in main is global.

Find more questions by tags C++