Shared Vector Variables Among Multiple C++ files -
i want share(globalize) vector variables(v1 , v2) between 2 cpp files(a.cpp , b.cpp). have defined both v1 , v2 in a.h following commands.
extern vector<uint64_t> v1; extern vector<uint64_t> v2;
i have added #include "a.h" both a.cpp , b.cpp files. can let me know else should able access elements of v1 , v2 in both of these cpp files?
thanks in advance
first, need pick place vectors should defined. let's choose a.cpp
.
in a.cpp
(only in 1 file - defining same object in multiple files yield multiple defined symbols error) define vectors global variables:
vector<uint64_t> v1; vector<uint64_t> v2;
in b.cpp
(and in other files want access v1
, v2
) declare vectors extern
. tell linker search elsewhere actual objects:
extern vector<uint64_t> v1; extern vector<uint64_t> v2;
now, in linking step v1
, v2
b.cpp
connected v1
, v2
a.cpp
(or whereever these objects defined).
Comments
Post a Comment