链接项目,我使用其他项目的静态成员

时间:2011-11-14 11:12:33

标签: c++ static linker

我遇到连接问题,我有两个项目可以兼容两个dll(A.dll B.dll)。在项目A中,我有静态Singleton singleton。 Psudocode: 项目A中的一个标题

ClassA
...
...
Singleton singleton;
...
...

在cpp文件中的项目B的某个地方,我有:

...
...
ClassA::singleton.SomeMethod();
...
...

项目编译,但链接存在问题。

我在 FreeCryEngine SDK 中遇到此问题 当我尝试在GameDLL项目中调用CCryAction::GetCryAction()时会发生这种情况。 这不起作用:

int a = CCryAction::GetCryAction()->IsInLevelLoad();
  

错误3错误LNK2001:未解析的外部符号“private:static   class CCryAction * CCryAction :: m_pThis“   (?m_pThis @ CCryAction @@ 0PAV1 @ A)E:\ CryENGINE_v3_3_5_2456_FreeSDK \ Code \ Game \ GameDll \ GameStateRecorder.obj GameDll

这种方法看起来如何?

static CCryAction * GetCryAction() { return m_pThis; }

1 个答案:

答案 0 :(得分:0)

您需要:在标题

struct ClassA {
    static Singleton singleton;
};

// or  
extern Singleton g_singleton;

在cpp文件中:

Singleton ClassA::singleton;

// or
Singleton g_singleton;

因此,您可以在包含标题的翻译单元中调用ClassA::singleton.someMethod()::g_singleton.someMethod()。请务必链接上面cpp的目标文件。