1

I have a class defined in headerfile x.h in dll-1 which is


class A{
   public:
   static int val;
__declspec(dllexport) static void setval(int v) {val = v;} 
};

and x.cpp has


int A::val = 256;

Now I built dll-1 and it went through, but the other dlls that link this dll failed to build with error unresolved external symbol public: static int A::val . This got resolved when I defined the exported function in x.cpp. but I don't understand why it broke. Thanks.

4

1 回答 1

4

因为您内联定义了函数,所以编译器可以在您调用它的地方自由地创建该函数的副本。这些副本无法看到变量,因为它没有被导出。

当您将函数定义移动到 .cpp 时,所有调用都会返回到 DLL,这可以在创建 DLL 时解析变量。

于 2012-09-25T23:11:59.750 回答