dllexport静态类方法

时间:2016-03-23 19:10:08

标签: c++ static dllexport

我正在使用Visual Studio并正在开发一些在类A中工作的C ++代码:

class A
{
public:
    //......
    static void foo();
};
导出

B

class __declspec(dllexport) B
{
public:
    //...
    void bar()
    {
        A::foo();
    }
};

AB被编译为AB.dll(和AB.lib)。 现在的主要计划:

int main()
{
    B b;
    b.bar();
    return 0;
}

When compiling the main program and linking it to AB.lib,
it complains about the A::foo() as unresolved external symbol
(in this case A::foo is a static function).
Do I need to somehow export A::foo() or could it be that 
I introduce errors somewhere?  Any help is appreciated.

Modified: 
1) Sorry for the type, it should be dllexport, not dllimport
2) in my actual project, the implementation is in .cpp files.  They are not inline functions.
Thanks.

1 个答案:

答案 0 :(得分:0)

我假设代码显示在你的.h头文件中,然后当你的头文件说:

class __declspec(dllimport) B
{
public:
    //...
    void bar()
    {
        A::foo();
    }
};

首先:你说B类是IMPORTED,它适用于你的主应用程序,但不适用于你的dll。

第二个:B :: bar()不是从dll导入的,而是直接在你的主应用程序中实现(编译器直接在你的头文件中读取,并且它没有尝试从dll导入)

Recomendations:

FIRST:重新定义您的头文件,如下所示:

class __declspec(dllimport) B
{
public:
    //...
    void bar();
};

在dll项目的cpp文件中实现方法B :: bar

SECOND:从头文件中删除A类(如果可以的话)

相关问题