通过 SWIG 从 Ruby 调用 Boost?

发布于 2024-10-02 19:54:37 字数 206 浏览 0 评论 0原文

假设我想从 Ruby 代码调用一个或多个 boost 库函数。我想这样做的例子有很多,但最近我想使用 Boost Graph 库来进行高效的图形处理。我认为最常建议执行此操作的方法是使用 SWIG。有没有人浏览过并生成 Boost 库的 SWIG 包装器?这实际上是与 Boost 互操作的最佳方式吗? Boost 的哪些部分无法通过 SWIG 访问,因为它们需要不受支持的 C++ 功能才能工作?

Suppose I want to call one or more boost library functions from Ruby code. There are many examples of times where I'd want to do this, but most recently I want to use the Boost Graph library for efficient graph processing. I think the way most often suggested for doing this is to use SWIG. Has anyone gone through and generated SWIG wrappers for the Boost library? Is this in fact the best way to interoperate with Boost? What parts of Boost cannot be accessed through SWIG because they require unsupported C++ features to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-10-09 19:54:37

我不能说我已经专门为 Boost 库生成了包装器,但我已经为我自己的模板类生成了包装器。

只要您记住 SWIG 将使用 %template 指令。

如上面链接的文档中所述:

[...] 裸露的 C++ 模板不定义 SWIG 通常可以为其创建包装器的任何类型的可运行对象代码。因此,为了包装模板,您需要提供有关特定模板实例化的 SWIG 信息(例如,vectorarray 等) 。其次,诸如vector之类的实例化名称在大多数目标语言中通常不是有效的标识符名称。因此,在创建包装器时,您需要为模板实例化指定一个更合适的名称,例如 intvector

使用该指令,您的 intvector 将如下所示:

%template(intvector) vector<int>;

I can't say I have generated wrappers for the Boost libraries specifically, but I have done so for my own templated classes.

This is perfectly doable as long as you keep in mind that SWIG will wrap specific template instantiations using the %template directive.

As stated in the documentation linked above:

[...] a bare C++ template does not define any sort of runnable object-code for which SWIG can normally create a wrapper. Therefore, in order to wrap a template, you need to give SWIG information about a particular template instantiation (e.g., vector<int>, array<double>, etc.). Second, an instantiation name such as vector<int> is generally not a valid identifier name in most target languages. Thus, you will need to give the template instantiation a more suitable name such as intvector when creating a wrapper.

With the directive your intvector would look something like this:

%template(intvector) vector<int>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文