I use MSVC to compile and link my program. I define a const variable in <A.cpp>,
//A.cpp
const bool bVal = true;
and I declare it in <B.cpp>.
//B.cpp
extern const bool bVal;
However, when I link the two object files, unresolved extern-symbol error on bVal is raised. So I use link.exe to check whether there is a mangled symbol of bVal in A.obj, and the answer is no.
So, is it a concrete rule that const global variable cannot be registered in symbol table or just a implementation of MSVC?
Oh, I find the answer. If you want detailed information, see here .
In short, constant global variable has internal linkage by default, use ‘extern’ specifier to make it external linkable.