NOTE: This only applies when the variables aren't const. I've undeleted this answer at the OP's request
static int x1
has internal linkage, meaning it's only accessible by name from the translation unit it's defined in (this cpp file). If this line is in a header file, every file that includes the header will get its own copy of the variable.
int x2
has external linkage, meaning another translation unit (cpp file) could have extern int x2;
in global scope and access the variable. If this line is in a header file, and multiple files include the header, you're violating the One Definition Rule since the variable is being defined once per include. You must instead use extern int x2;
in the header, and then in a single cpp file have your int x2 = ...;
to avoid multiple definitions.
See https://en.cppreference.com/w/cpp/language/storage_duration