在没有静态方法的情况下创建 C++ DLL [英] Creating c++ DLL without static methods

查看:16
本文介绍了在没有静态方法的情况下创建 C++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C++ 创建一个 DLL.下面是一个例子:

I am creating a DLL in C++. Here is an example:

namespace MathFuncs
{
  class MyMathFuncs
  {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}

所有方法都是静态的,静态方法有很多限制,所以我的问题是如何在没有静态方法的情况下实现相同的方法?我总是需要在 DLL 中有静态方法吗?我想在 C# 和 IOS 应用程序中导入这个 DLL.

All methods are static and there are lot of limitations with static methods so my question is how can I implement the same without static methods? Do I always need to have static methods in DLL? I want to import this DLL in C# and IOS apps.

推荐答案

作为旁注,我几天前用 mingw/c++ 做了 1 个实验,可以为您澄清.

As side note I did 1 experiment few days ago with mingw/c++ wich can be clarifying for you.

我有一个全局引用计数器,用于查找程序中的内存泄漏,

I had a Global reference counter for find out memory leaks in my program,

class ReferenceCounter /** other implementations details are omitted.*/
{
public:

static int GlobalReferenceCounter;

//version 1
static int getReferenceCount1() { return GlobalReferenceCounter;}

//verison 2
static int getReferenceCount2(); //same code of version 1 but moved into .cpp file
};

当我使用引用计数器将我的库编译成 DLL 时,变量被复制,1 个版本被编译到 DLL 中,一个版本在客户端代码中编译.

When compiling my library using reference counter into a DLL, then the variable is duplicated, 1 version is compiled into the DLL, and one version is compiled in client code.

当我从 DLL 的工厂方法中询问引用计数的东西时,只有 DLL 内的引用计数器会增加/减少.当客户端代码使用从 Ref Counter 继承的自己的类时,客户端引用计数器会增加/减少.

When I ask istances of reference counted stuff from the factory methods of the DLL, only the reference counter inside the DLL is increased/reduced. When client code is using its own classes inherited from Ref Counter, then the client reference counter is increased/reduced.

所以为了检查内存泄漏,我必须在程序结束时进行

So for check for memory leaks I must do at end of the program

assert(ReferenceCounter.getReferenceCount1() == 0);
assert(ReferenceCoutner.getReferenceCount2() == 0);

那是因为在内存泄漏的情况下,其中一个值会大于0.如果第一个值大于1,则内存泄漏是由未分配的用户类引起的,如果第二个值大于0,则内存泄漏是由库类引起.

that's because in case of memory leak one of those values will be greater than 0. If first value greater than 1, then the memory leak is caused by unallocated user classes, if second value is greater than 0, then memory leak is caused by library classes.

请注意,如果泄漏是由未分配的库类引起的,则这不一定是库的错误,因为用户仍然可以泄漏该类,即使这意味着库中缺乏设计,因为理想情况下为了安全起见,应该以适当的智能指针返回.)

Note that if leak is caused by unallocated library's classes, this is not necessarily a bug of the library, since user is still able to leak that classes, even if that should mean a lack of design in the library, since ideally everythin should be returned in proper smart pointers for safety.)

当然,您应该在文档中指定GlobalReferenceCoutner"是重复的,否则一些不知情的用户可能会认为 2 个 getter 是多余的,并会认为您犯了一些错误.(如果可能的话,避免做这样的事情,是晦涩难懂的)

Of course you should specify that "GlobalReferenceCoutner" is duplicated in the documentation, else some unaware user can just think that 2 getters are redundant and will think you did some mistake. (if possible avoid to do something like that, is obscure and unclear)

这也应该警告您,通过 DLL 访问静态方法是非常不安全的.例如,如果在我的班级中我只想有 1 个引用计数器而不是 2 个,我必须这样做以确保安全:

That should also warn you that accessing static method through DLLs is highly unsafe. For example If In my class I wanted to have only 1 reference counter instead of 2 I had to do that for grant safety:

class ReferenceCounter
{
public:

static int GlobalReferenceCounter;

static void duplicate() { increaseGlobalCounter(); }

static void release() { decreaseGlobalCounter(); }

static int getGlobalCounter() { return privateGetGlobalCounter(); }

private:

static increaseGlobalCounter(); // implementation into Cpp file

static decreaseGlobalCounter(); // implementation into Cpp file

static privateGetGlobalCounter(); // implementation into Cpp file

};

这样做会让您在 DLL 边界和用户应用程序中使用相同的值.因此,这里没有使用 2 个不同的变量,而是使用 1 个变量(可能 GlobalCounter 仍被编译为用户可执行文件,但没有人使用它来消除克隆效果")

doing that will grant you that the same value is used across DLL bounduaries and in User application. so instead of have 2 different variables here we use 1 variable (probably the GlobalCounter is still compiled into user executable, but no one is using it removing the "clone effect")

这篇关于在没有静态方法的情况下创建 C++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆