DLL导出静态类方法

dllexport static class methods

本文关键字:静态类 类方法 静态 DLL      更新时间:2023-10-16

我正在使用Visual Studio,并且正在处理一些C++代码,这些代码在第A类中的工作方式如下:

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.

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

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

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

第二:B::bar(( 不是从 DLL 导入的,而是直接在主应用中实现的(编译器直接在头文件中重新添加,并且不会尝试从 DLL 导入(

推荐:

首先:像这样重新定义你的头文件:

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

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

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