Reputation: 1
I have code in two files like:
Static.cpp:
static int s_Variable = 5;
Main.cpp:
extern int s_Variable;
I get this error:
Build started...
1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>Static.cpp
1>Main.obj : error LNK2001: unresolved external symbol "int s_Variable" (?s_Variable@@3HA)
1>E:\Visual Studio 2019\Dev\CPPSeries\HelloWorld\bin\Win32\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "HelloWorld.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Everything compiles properly on each file, so I know it is just a linking issue. What exactly is wrong?
Upvotes: 0
Views: 117
Reputation: 847
Static global variables do not have external linkage; in a sense, they are "local" to a given translation unit. You would have to use a non-static global variable if you wanted to refer to it in a separate translation unit.
Upvotes: 6