【问题标题】:How to pass object from D to C++?如何将对象从 D 传递到 C++?
【发布时间】:2016-03-23 23:00:37
【问题描述】:

我正在尝试与 C++ 和 D 进行互操作。我今天发现的事情真的让我很困惑:对象没有在我的程序中正确传递。

最好举个例子。

我有一个 C++ 库,我将其编译为目标文件和 D 程序,我将其与我的库链接并运行。

他们在这里:

#include <stdio.h>

class Color
{
public:
  Color(unsigned int _r, unsigned int _g, unsigned int _b) : r(_r), g(_g), b(_b) {}

  unsigned int r, g, b;
};

class Printer
{
public:
  Printer() {}
  ~Printer() {}
  static Printer* getInstance();
  void print(Color *c);
};

Printer* Printer::getInstance()
{
  return new Printer();
}

void Printer::print(Color *c)
{
  printf("(%d, %d, %d)\n", c->r, c->g, c->b);
}

还有D程序:

import std.stdio;

extern(C++)
{
  class Color
  {
    uint r, g, b;

    this(uint _r, uint _g, uint _b)
    {
      r = _r;
      g = _g;
      b = _b;
    }
  }

  class Printer
  {
    @disable this();
    static Printer getInstance();
    final void print(Color c);
  }
}

void main()
{
  auto printer = Printer.getInstance();

  Color c = new Color(42, 7, 19);

  printer.print(c);
}

我用这些命令编译它们:

c++ -c my_core.cpp -o my_core.o
dmd main.d my_core.o -L-lstdc++

但是当我运行./main 时,我得到了奇怪的结果:

(113244372, 1, 42)

让我认为对象被错误传递的只是一个简单的实验。首先,我运行了我的程序几次,结果如下:

$ ./main
(266442332, 1, 42)
$ ./main
(234899036, 1, 42)
$ ./main
(109475420, 1, 42)

所以第一个数字似乎是一个指向内存块的指针。加上汇编知识,我的第六感让我认为它是指向this 变量的指针。

现在,为了确认我的数据仍然存在并且这些数字不只是随机数字,我在我的班级中添加了另外两个字段Color

C++ 库:

#include <stdio.h>

class Color
{
public:
  Color(unsigned int _r, unsigned int _g, unsigned int _b, unsigned int _u, unsigned int _v) : r(_r), g(_g), b(_b), u(_u), v(_v) {}

  unsigned int r, g, b, u, v;
};

class Printer
{
public:
  Printer() {}
  ~Printer() {}
  static Printer* getInstance();
  void print(Color *c);
};

Printer* Printer::getInstance()
{
  return new Printer();
}

void Printer::print(Color *c)
{
  printf("(%d, %d, %d, %d, %d)\n", c->r, c->g, c->b, c->u, c->v);
}

还有D程序:

import std.stdio;

extern(C++)
{
  class Color
  {
    this(uint _r, uint _g, uint _b, uint _u, uint _v)
    {
      r = _r;
      g = _g;
      b = _b;
      u = _u;
      v = _v;
    }

    uint r, g, b, u, v;
  }

  class Printer
  {
    @disable this();
    static Printer getInstance();
    final void print(Color c);
  }
}

void main()
{
  auto printer = Printer.getInstance();

  Color c = new Color(42, 7, 19, 499, 727);

  printer.print(c);
}

输出是:

$ ./main                         
(90379876, 1, 42, 7, 19)
$ ./main
(79758948, 1, 42, 7, 19)
$ ./main
(74901092, 1, 42, 7, 19)
$ ./main
(217458276, 1, 42, 7, 19)
$ ./main
(238933604, 1, 42, 7, 19)

我尝试使用 DMD 和 LDC 编译器来编译我的程序,但两者都为我提供了完全相同的行为。

UPD:更有趣的是,(可能)指出了问题所在,事实上,在 C++ 库中创建的对象正在 D 和 C++ 之间传递正确。

为了证明这一点,我在 Color 类中创建了“工厂方法”:

static Color* create(unsigned int _r, unsigned int _g, unsigned int _b, unsigned int _u, unsigned int _v) {
    return new Color(_r, _g, _b, _u, _v);
}

然后,在 D 程序中:

Color c = Color.create(42, 7, 19, 499, 727);

printer.print(c);

所以c对象来自C++库,被传递给printer对象,在C++库中创建,这个传输是在D程序中进行的。

而且结果出乎意料地正确:

$ ./main
(42, 7, 19, 499, 727)

我是否缺少 C++ 和 D 互操作的概念,或者这是两个 D 编译器中的错误(怀疑)

【问题讨论】:

  • AFAIK 这是设计使然。 D 能够与 C++ 交互,但它不能自己创建 C++ 对象(并且 D 对象具有不同的二进制布局)。使用工厂方法似乎是正确的。

标签: c++ interop d dmd ldc


【解决方案1】:

你不应该使用 Ds new 来分配 C++ 类,如果你创建一个 Color::getInstance 它可以工作。

import std.stdio;

extern(C++)
{
  class Color
  {
    this(uint _r, uint _g, uint _b, uint _u, uint _v)
    {
      r = _r;
      g = _g;
      b = _b;
      u = _u;
      v = _v;
    }

    uint r, g, b, u, v;
    static Color getInstance(uint _r, uint _g, uint _b, uint _u, uint _v);
  }

  class Printer
  {
    @disable this();
    static Printer getInstance();
    final void print(Color c);
  }
}

void main()
{
  auto printer = Printer.getInstance();
  auto c = Color.getInstance(42, 7, 19, 499, 727);

  printer.print(c);
}

#include <stdio.h>

class Color
{
public:
  Color(unsigned int _r, unsigned int _g, unsigned int _b, unsigned int _u, unsigned int _v) : r(_r), g(_g), b(_b), u(_u), v(_v) {}

  unsigned int r, g, b, u, v;
  static Color* getInstance (unsigned int _r, unsigned int _g, unsigned int _b, unsigned int _u, unsigned int _v);
};

Color* Color::getInstance(unsigned int _r, unsigned int _g, unsigned int _b, unsigned int _u, unsigned int _v)
{
  return new Color(_r, _g, _b, _u, _v);
}

class Printer
{
public:
  Printer() {}
  ~Printer() {}
  static Printer* getInstance();
  void print(Color *c);
};

Printer* Printer::getInstance()
{
  return new Printer();
}

void Printer::print(Color *c)
{
  printf("(%d, %d, %d, %d, %d)\n", c->r, c->g, c->b, c->u, c->v);
}

【讨论】:

  • 但这听起来像是绕过问题,不是吗?.. D 文档中有一个参考:dlang.org/spec/cpp_interface.html#memory-allocationLeaving a pointer to it on the stack (as a parameter or automatic variable) (...是一个解决方案有点问题...)
  • 那是为分配给 GC 的对象防止它们被收集,因为 GC 不知道 C++ 仍然有对这些对象的引用。例如,如果您将 GC 分配的对象存储在 C++ 数组中。
猜你喜欢
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多