I’m pretty new to Visual Studio and I’m currently working on a project in which I want to use multiple .cpp files. Basically I want to make a function outside main.cpp in function.cpp and that function should be able to change global variables. Then I’d use that function in main.cpp.
I tried making a header named globals.h and putting static variables in it. I included globals.h in both main and function.cpp and it compiled but whenever I call that function in main it does absolutely nothing.
When I try to include function.cpp in main.cpp I get multiple definition error while compiling.
What am I doing wrong? Thanks in advance!
Don’t use
staticvariables in header files. As headers are “incorporated” in compilation units, all variables declared asstaticin your header become scoped inside your compilation unit only. You won’t be able to use the same global variable in your cpp files.This is how your structure should be: