用ctypes包装C ++库不是一个好主意吗? [英] Is wrapping C++ library with ctypes a bad idea?

查看:88
本文介绍了用ctypes包装C ++库不是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在包装C库中通读了以下两个线程 C ++库,我不确定是否得到还没完成.我正在使用的C ++库确实使用了类和模板,但没有以任何过于复杂的方式使用.用ctypes包装它有什么问题或警告(除了可以在纯python等中实现的点之外)?

I read through the following two threads on wrapping C library and C++ library, I am not sure I get it yet. The C++ library I am working with does use class and template, but not in any over-sophisticated way. What are issues or caveats of wrapping it with ctypes (besides the point that you can do so in pure python etc)?

PyCXX,Cython和boost :: python是人们提到的另外三个选择,是否有共识是哪个更适合C ++?

PyCXX , Cython and boost::python are three other choices people mentioned, is there any consensus which one is more suitable for C++?

谢谢

橄榄

推荐答案

要使C ++库可以从Python访问,它必须使用C导出名称,这基本上意味着名为foo的函数可以通过foo.

For C++ a library to be accessible from Python it must use C export names, which basically means that a function named foo will be accessible from ctypes as foo.

这只能通过export C {}封闭公共接口来实现,而export C {}则不允许函数重载和其中的模板(只有要包装的库的公共接口才是相关的,内部并非如此,并且可能会使用他们喜欢的任何C ++功能.)

This can be achieved only by enclosing the public interface with export C {}, which in turn disallows function overloading and templates therein (only the public interface of the library to be wrapped is relevant, the inner workings are not and may use any C++ features they like).

这样做的原因是C ++编译器使用一种称为 name mangling 的机制来为重载或模板化的符号生成唯一的名称.尽管ctypes仍然可以找到一个函数,只要您知道它的错误名称,该错误方案就取决于所使用的编译器/链接器,而您可以依靠的内容不多. 简而言之:请勿使用ctypes封装在其公共接口中使用C ++功能的库.

Reason for this is that C++ compilers use a mechanism called name mangling to generate unique names for overloaded or templated symbols. While ctypes would still find a function provided you knew its mangled name, the mangling scheme depends on the compiler/linker being used and is nothing you can rely on. In short: do not use ctypes to wrap libraries that use C++ features in their public interface.

Cython采用不同的方法.它有助于您构建与原始库进行接口的C扩展模块.因此,通过常规的C ++链接机制可以链接到C ++库,从而避免了上述问题. Cython的麻烦在于,需要针对每个平台重新编译C扩展库,但是无论如何,这也适用于要包装的C ++库.

Cython takes a different approach. It aids you at building a C extension module that does the interfacing with the original library. Therefore, linking to the C++ library is done by the regular C++ linkage mechanism, thus avoiding the aforementioned problem. The trouble with Cython is that C extension libraries need to to be recompiled for every platform, but anyway, this applies to the C++ library to be wrapped as well.

就个人而言,我想说的是,在大多数情况下,启动Cython的时间是花了很多时间,与ctypes相比,它最终将获得回报(真正简单的Cish接口除外).

Personally, I'd say that in most cases the time to fire up Cython is a time that is well-spent and will eventually pay off in comparison to ctypes (with an exception for really simple Cish interfaces).

我对boost.python没有任何经验,所以我无法对此发表评论(但是,我也不认为它很受欢迎).

I don't have any experience with boost.python, so I can't comment on it (however, I don't have the impression that it is very popular either).

这篇关于用ctypes包装C ++库不是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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