0

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?

  1. Am I even allowed to do this? That is, have static const data defined in several different dlls, and expect it to work?
  2. I tried taking out the assignments, and putting them into a separate .cpp file. But some of the code uses the static const vars in case statements. That works fine until I take out the assignment; then case says it needs a constant expression (of course).
  3. I can't inline these; VS says that's an error.
  4. 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.

MJF
  • 83
  • 9
  • Did you actually define the member in an implementation file? `const U32 J::SA_UNSPECIFIED;` – aschepler May 31 '17 at 14:15
  • No. I assumed (uh-oh!) that as it was `static const int` I didn't need to. I did try to do that (define in an implementation file and take out of the `.h'), but then the variables can no longer be used in a `case`. – MJF May 31 '17 at 14:58
  • You can leave the `= 1` in the header file so that the value can still be used, but you still need the definition (with no initializer) in the source file. – aschepler May 31 '17 at 22:16

0 Answers0