"deflagg" <deflagg@discussions.microsoft.com> wrote in message
news:0F66DB6F-0BA8-4A35-94CC-47F9A52852C6@microsoft.com...
DllTest.h
#include <iostream>
using namespace std;
class __declspec(dllexport) CDllTest
{
public:
CDllTest(){}
~CDllTest(){}
void foo(){myint = new int();}
static int* myint;
};
DllTest.cpp
int* CDllTest::myint = NULL;
This code is in a DLL called DllTest. When I try to link this dll to my
client app, I get a linker error. It is as follows:
Error 1 error LNK2001: unresolved external symbol "private: static int *
CDllTest::myint" (?myint@CDllTest@@0PAHA) DllTestApp.obj
I figured out if I put the intialization of myint into the header file
along
with the class definition, it will link fine. It's only when I put the
intialization of myint in the cpp file. Yes I could just keep the
initialization in my cpp file, but that's not the point. The point is,
why I
can't have it in my cpp file vs my header file. Thank's in advance for
all
your help.
You've mixed DLL exports and C++ access control in a way that's
incompatible.
Since you've written the definition of foo() inline, it'll be compiled in
your client, but since CDllTest::myint is private, it won't be exported by
the DLL. When you initialize the static in the class definition, then the
compiler can see the constant initialization and doesn't need the address of
the actual static variable.
In a more realistic application, you'd have the implementation of
CDllTest::foo() out of line in DllTest.cpp where it'd be able to access the
private member.
-cd
I moved the definition of foo from inline to the cpp file. Now it will