Programe #1

// file.h
class File
{
public:
static const int var = 9;
};
-

// main.cpp
#include <iostream>
#include "file.h"
using namespace std;

int main() {
File f;
cout << f.var;
return 0;
}
Programe #2

// file.h
int Globalvar ;
class File
{
public:
static const int var = 9;
};
-

// main.cpp
extern int GlobalVar;

#include <iostream>
#include "file.h"
using namespace std;

int main() {
cout << GlobalVar;
return 0 ;
}
Program#1 is running fine, but program#2 gives linker error:

error LNK2005: "int GlobalVar" (?x@@3HA) already defined in file.obj
I know the header files are never compiled. Then in the above case, how the compiler knows the definition of variable var, but not able to find the definition of GlobalVar? What is the difference between this two programs?