全局的.dll头文件

Global in .dll Header

本文关键字:文件 dll 全局      更新时间:2023-10-16

我有一个.dll头声明一个类。

在类声明之后实例化类的静态对象。

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

当第一次调用这些导出函数之一返回时,我得到一个无法解释的段错误。所以我的问题是:是可以声明一个静态对象在.dll头像这样:

class Foo{
public:
    void bar();
};
static Foo foo;
__declspec( dllexport ) void func() { foo.bar(); }

对于您正在尝试的,您需要从头文件中完全删除类,它根本不属于那里。

试试这样写:

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...