It is important to realize that
extern Pub* gpub;
is not a declaration of a variable, but a declaration of an external object. To create a valid program, you need an actual declaration (i. e. without 'extern') elsewhere.
If your program just consists of:
int a;
int main() {
a = 1;
return a;
}
then all is well, because the first line is a (normal) declaration.
If your program consists of
extern int a;
int main() {
a = 1;
return a;
}
then the linker will complain that it cannot find any declaration of a, and therefore doesn't know how to specify a memory address that the program main should use when referencing a. (the compiler doesn't know that - it only cares to know that a is an object of type int)
Note that using extern only really makes sense when you create a library that is used by another program. If used in a library, the statement extern int a; will tell the linker to create external linkage to the object a, so that the actual program knows where to find this object.
So, another way to fix the second code example would be to create a library 'mylib.lib' with a header file containing
"extern int a;"
and a source file containing
#include <mylib.h>
int a = 1;
and then write your main program as
#include <mylib.h> // contains the extern declaration
int main() {
return a;
}
This setup will tell the compiler the type of a, and the linker will know that the declaration may not be contained in your main.exe. Linking main.exe to mylib.lib then allows the linker to find the external linkage declaration in mylib.lib.