简体   繁体   English

如何从 Python 代码中导出 .so 符号

[英]How to export .so symbols from Python code

I need to code some functionality in Python 3.8 and comply to a given C++ Interface.我需要在 Python 3.8 中编写一些功能并遵守给定的 C++ 接口。 The code should replace an existing .so file which exposes this interface that is defined in a C++ header.该代码应替换现有的.so文件,该文件公开在 C++ 标头中定义的此接口。

Here's the two options I considered so far:这是我目前考虑的两个选项:

  1. Building a wrapper .so file by coding it in C++ including the interface header and delegating each call to Python with PyEval_CallObject(...)通过在 C++ 中编码包括接口头并使用PyEval_CallObject(...)每次调用委托给 Python 来构建包装器.so文件
  2. Using Cython to compile an .so from a .pyx trying to export the needed symbols使用用Cython编译的.so.pyx试图出口所需的符号

Option 1 would most probably work, but seems a lot of work for a dumb wrapper.选项 1 很可能会起作用,但对于一个愚蠢的包装器来说似乎需要做很多工作。 There must be a better way.一定会有更好的办法。 I couldn't get option 2 to work, and I don't know if that's possible at all.我无法让选项 2 起作用,我不知道这是否可行。

Happy to hear about any other ideas and hints.很高兴听到任何其他想法和提示。

In the end I picked 1. as the solution but using a nice library called pybind11 ( https://github.com/pybind/pybind11 ).最后,我选择了 1. 作为解决方案,但使用了一个名为pybind11 ( https://github.com/pybind/pybind11 ) 的不错的库。 It really simplifies both interfacing Python from c++ and c++ from Python especially when it comes to type conversions.它确实简化了从 c++ 连接 Python 和从 Python 连接 c++ 的接口,尤其是在类型转换方面。 In this way the wrapper .so file became really small.通过这种方式,包装器.so文件变得非常小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM