I'm trying to create a global variable, within my VS2017 solution, to share an integer between (2) CPP files. I thought this was a no brainer, but for the life of me I can't shake this darn LNK1120 error. This should be so easy, please tell where I'm screwing up.
I tried a very simple program, that doesn't work for me (get LNK1120). Is there something specific that needs to be setup in VS2017? I have tried making the cpp files dependent on each other but no dice ...
global.cpp:
int g_x;
int g_y(2);
main.cpp:
#include <iostream>
extern int g_x;
int main()
{
extern int g_y;
g_x = 5;
std::cout << g_y; // should print 2
return 0;
}
The above example is from this website (https://www.learncpp.com/cpp-tutorial/42-global-variables/)
The code compiles okay, but the Linker keeps throwing the errors below;
Error LNK2001 unresolved external symbol "int g_x" (?g_x@@3HA)
Error LNK2001 unresolved external symbol "int g_y" (?g_y@@3HA)
Error LNK1120 2 unresolved externals
This leads me to believe that there is something that needs to be setup in the Linker configuration settings? What am I missing here?