首页 文章

全球在.dll Headers

提问于
浏览
0

我有一个.dll头,它声明了一个类 .

在类声明之后,它实例化该类的静态对象 .

.dll导出的函数与静态对象的接口 .

当第一次调用其中一个导出函数返回时,我得到一个莫名其妙的段错误 . 所以我的问题是: Is it OK to declare a static object in a .dll header like this:

class Foo{
public:
    void bar();
};

static Foo foo;

__declspec( dllexport ) void func() { foo.bar(); }

1 回答

  • 1

    对于您正在尝试的内容,您需要从头文件中删除该类,它根本不属于那里 .

    尝试更像这样的东西:

    MyDll.h(与想要使用你的DLL的项目共享):

    #ifndef MyDllH
    
    #ifdef BUILDING_DLL
    #define MYDLL_EXPORT __declspec( dllexport )
    #else
    #define MYDLL_EXPORT __declspec( dllimport )
    #endif
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    MYDLL_EXPORT void func();
    // other functions as needed...
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    

    MyDll.cpp :(仅作为DLL项目的一部分编译):

    #define BUILDING_DLL
    #include "MyDll.h"
    
    class Foo
    {
    public:
        void bar();
    };
    
    void Foo::bar()
    {
        //...
    }
    
    static Foo foo;
    
    void func()
    {
        foo.bar();
    }
    
    // other functions as needed...
    

相关问题