访问C ++库的通用方法? [英] generic way to access C++ libs?

查看:33
本文介绍了访问C ++库的通用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何通用的方法可以在Python中使用C ++库。


我似乎记得有工具可以为C库生成包装器

半自动。


但这些仍然过于繁琐,恕我直言。


我想要的是一些模块(或无论如何,我可以用它来表示加载这个C ++库,然后创建那个C ++对象。或者调用

方法x的C ++对象y。


不做任何其他事情(例如重新编译库或生成

包装器。


我同意模板可能会成为一个主要问题,所以如果

它与预先实例化的模板一起工作,我会很高兴。


还有什么吗?

Gab。


-

/ - -------------------------------------------------- --------------------- \

|有等待的作品,|

|哪一个人长时间不理解; [...] |

|对于这个问题,经常在答案后很长时间内到达。 |

| (奥斯卡王尔德)|

+ ------------------------------------ ------------------------------------- +

| za**@cs.uni-bonn.de __ @ /'' www.gabrielzachmann.org |

\ -------------- -------------------------------------------------- --------- /

Is there any generic way to use C++ libraries from within Python.

I seem to recall that there are tools to generate wrappers for C-libraries
semi-automatically.

But those were still way too cumbersome, IMHO.

What I would like to have is some module (or whatever), with which I can
say "load this C++ library", and then, "create that C++ object" or "call
method x of C++ object y".

Without doing anything else (such as recompiling the library or generating
wrappers).

I agree that templates could pose a major problem, so I would be happy if
it worked with pre-instantiated templates.

Is there anything?
Gab.

--
/-------------------------------------------------------------------------\
| There are works which wait, |
| and which one does not understand for a long time; [...] |
| for the question often arrives a terribly long time after the answer. |
| (Oscar Wilde) |
+-------------------------------------------------------------------------+
| za**@cs.uni-bonn.de __@/'' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/

推荐答案

>我想要的是一些模块(或其他),我可以
> What I would like to have is some module (or whatever), with which I can
说加载此C ++库,然后创建该C ++对象。或调用
C ++对象y的方法x。


C ++没有运行时类型信息的概念(除了说这是类X的
)。这种信息被称为反射。在java或

python中,它允许对这些语言进行后期绑定。在其他

字样中:你可以为它们创建动态包装器。


但是c ++缺少这意味着你需要提供

objecst - 头文件 - 到某个生成器。这正是在swig和sip这样的包装器生成器中发生的事情。我从来没有玩过
,但是我不相信他们让不可能变为可能。


所以回答你的问题:这是不可能的。还有其他原因如:
好​​:C ++没有定义对象的二进制布局,结果是lib

符合不同的编译器(gcc,intel,msvc)导致

不兼容的二进制文件。因此,即使有人会使用标题

文件发送lib:要实际生成包装器,相应的compliler必须使用
。由于你很少在终端用户上找到不同种类的编译器,你必须在开发者机器上进行。

不做任何其他事情(例如重新编译库或生成
包装器。
say "load this C++ library", and then, "create that C++ object" or "call
method x of C++ object y".
C++ has no concept of runtime type information (except from saying "this is
of class X). This sort of information is known as "reflection" in java or
python, and that allows for late time bindings to those languages. In other
words: you can create on the fly wrappers for them.

But c++ lacking this means that you need to feed the specification of
objecst - the header files - to some generator. WHich is exactly what
happens in the wrapper generators like swig and sip. I never toyed around
with boost, but I don''t believe they made the impossible possible.

So to answer your question: Its not possible. There are other reasons as
well: C++ defines no binary layout of objects, the result is that a lib
complied with different compilers (gcc, intel, msvc) results in
incompatible binaries. So even if one would ship the lib with the header
files: to actually generate the wrappers, the appropriate compliler has to
be used. And as you rarely find compilers of different kinds on end user
machines, you''ll have to do it on the developers machine.
Without doing anything else (such as recompiling the library or generating
wrappers).




正如我刚才所说:不可行。


第三件事是因为语言设计的差异使得有必要将开发人员的时间用于创建包装:在c ++中,您可以通过指向它们的指针传递参数

,允许在呼叫者堆栈帧(或者他们住的时候是
)。因为这在python中是不可能的,你需要在这周围工作

,通常是通过返回一个修改值的元组除了

方法/函数本身的结果。


因此自动生成封装器有很大的限制 - 必须完成
最小工作量。

-

问候,


Diez B. Roggisch



As I just said: not doable.

Third thing is that differences in language design make it necessary to
invest developer time in creating wrappings: In c++ you can pass arguments
by a pointer to them, allowing to modify them in the callers stackframe (or
whereever they live). As this is not possible in python, you need to work
around this, usually by returning a tuple of modified values in addition to
the result of the method/function itself.

So there are strong limits on automatically generationg wrappers - a
minmimum of work has to be done.
--
Regards,

Diez B. Roggisch


C ++没有运行时类型信息的概念(除了说这是
是X类)。这种信息被称为反射。在java



让'我们假设我们在lib中有一个很好的符号表

(通常是这种情况,或者,至少,不太严格的限制)。

所以回答你的问题:这是不可能的。还有其他原因:C ++没有定义对象的二进制布局,结果是lib
遵循不同的编译器(gcc,intel,msvc)会导致不兼容的二进制文件。因此,即使有人将带有标题
C++ has no concept of runtime type information (except from saying "this
is
of class X). This sort of information is known as "reflection" in java
or
let''s assume we have a well-populatyed symbol table in the lib
(which is usually the case, or, at least, not too hard a restriction).
So to answer your question: Its not possible. There are other reasons as
well: C++ defines no binary layout of objects, the result is that a lib
complied with different compilers (gcc, intel, msvc) results in
incompatible binaries. So even if one would ship the lib with the header



的lib发送,但这不是真的。

实际上,每个平台(wintel, linux,...)有一个非常明确的

目标文件格式(例如unix / linux下的ELF)。

当前icc / linux和gcc / g ++在大多数情况下工作得很好,并且

icc / windows和cl。


很明白,设想的python模块必须是

特定于平台。

祝你好运,

gabriel。

-

/ ------------------------------------------------ ------------------------- \

|有等待的作品,|

|哪一个人长时间不理解; [...] |

|对于这个问题,经常在答案后很长时间内到达。 |

| (奥斯卡王尔德)|

+ ------------------------------------ ------------------------------------- +

| za**@cs.uni-bonn.de __ @ /'' www.gabrielzachmann.org |

\ -------------- -------------------------------------------------- --------- /



that''s not quite true.
actually, each platform (wintel, linux, ...) has a pretty well-defined
object file format (ELF under unix/linux, for instance).
current icc/linux and gcc/g++ work pretty well together in most cases, and
icc/windows and cl, too.

It is well understood that the envisioned python module would have to be
platform-specific.
Best regards,
gabriel.
--
/-------------------------------------------------------------------------\
| There are works which wait, |
| and which one does not understand for a long time; [...] |
| for the question often arrives a terribly long time after the answer. |
| (Oscar Wilde) |
+-------------------------------------------------------------------------+
| za**@cs.uni-bonn.de __@/'' www.gabrielzachmann.org |
\-------------------------------------------------------------------------/


>让我们假设我们在lib
> let''s assume we have a well-populatyed symbol table in the lib
中有一个很好的符号表(通常是这种情况,或者,至少不是太严格的限制)。


那又怎样?名称被破坏了 - 每个人根据其编译器拥有

规则。即使符号本身已定义:没有数据类型

结构和类本身的结构布局存储在符号

表中。

事实并非如此。
实际上,每个平台(wintel,linux,......)都有一个非常明确的目标文件格式(例如,unix / linux下的ELF)。


我没有谈论二进制可执行格式,而是C ++的内存布局

对象。 c ++标准没有定义例如一个

对象虚拟方法的vtable驻留 - 或者即使虚拟方法必须由vtable在第一个位置实现。


由g ++创建的c ++对象是传递给VC库的完全垃圾。



当前icc / linux和gcc / g ++在大多数情况下都能很好地协同工作案件,以及icc / windows和cl。
(which is usually the case, or, at least, not too hard a restriction).
So what? The names are mangled - and each one according to its compilers own
rules. Even if symbols themselves are defined: there is no data type
structure layout for structs and classes themselves stored in the symbol
table.
that''s not quite true.
actually, each platform (wintel, linux, ...) has a pretty well-defined
object file format (ELF under unix/linux, for instance).
I did not talk about binary executable formats, but the memory layout of C++
objects. The c++ standard doesn''t define where e.g. the vtable of an
objects virtual method resides - or even if virtual methods have to be
implemented by a vtable at first place.

A c++ object created by g++ is total garbage passed to a VC lib that appears
to
current icc/linux and gcc/g++ work pretty well together in most cases, and
icc/windows and cl, too.




不,他们不 - 不是C ++代码。谷歌的名称损失和原因

为什么每个编译器都使用自己的方案。英特尔声称他们的编译器和gcc之间存在二进制

兼容性,但这仅适用于某些编译器版本的

- 您无法控制的使用情况
计划方案。


我建议你先学习更多关于c ++代码的问题

代和那些试图开发的人遇到的困难图书馆

for c ++(运送给客户/用户的库不是为了他们自己的项目

当然)。这就是为什么只有很少的c ++ libs出现的一个主要原因 - 例如
trolltech必须经历不破二进制

不同版本之间的兼容性可以在这里观察到:

http://developer.kde.org/documentati...atibility.html

-

问候,


Diez B. Roggisch



No, they don''t - not for C++ code. Google for name mangling and the reasons
why every compiler uses its own scheme. Intel claims that there is binary
compatibility between them compiler and gcc, but thats only true for
certain compiler versions - which usage you have no control of in your
planned scenario.

I suggest you first delve somewhat more on the subject of c++ code
generation and difficulties observed by those trying to develop libraries
for c++ (libs that are shipped to customer/users not for their own projects
of course). Thats one major reason why there are only few c++ libs out
there - the pains e.g. trolltech has to go through to not break binary
compatibility between different versions can be observed here:

http://developer.kde.org/documentati...atibility.html
--
Regards,

Diez B. Roggisch


这篇关于访问C ++库的通用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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