I'm working on a legacy VS2015 app that creates several DLLs. They all include a certain .h file that declares/defines static data. After preprocessing that becomes:
class __declspec(dllexport) J { public: static const unsigned int SA_UNSPECIFIED = 1; ... };
In my app .cpp
file I include the same .h file, and that preprocesses (#ifdef
, etc.) to this:
class __declspec(dllimport) J { public: static const unsigned int SA_UNSPECIFIED = 1; ... };
My main symptom is that my app says that during link it cannot find J::SA_UNSPECIFIED
. The app "References" (using the "Reference" tree in VS) these other DLLs that export J
.
I think this is different from "Why static variable needs to be explicitly defined?" in that I'm using const integral, as opposed to the general case. Is this not true in this case?
- Am I even allowed to do this? That is, have
static const
data defined in several different dlls, and expect it to work? - I tried taking out the assignments, and putting them into a separate .cpp file. But some of the code uses the
static const
vars incase
statements. That works fine until I take out the assignment; thencase
says it needs a constant expression (of course). - I can't
inline
these; VS says that's an error. - Other developers in the group, using identical code and project files, can build without error. I haven't yet compared VS versions.
Answer:
It turns out that in my app I use SA_UNSPECIFIED
in a non-integral way, and so can no longer be initialized in the class definition.