如何从用 C 或 C 编写的代码创建 make .so 文件可以从Python使用

发布于 2024-08-28 07:11:21 字数 248 浏览 6 评论 0 原文

查看 Python 模块和 Python 框架中“lib-dnyload”目录中的代码,我注意到每当代码创建某种 GUI 或图形时,它都会导入带有 .so 扩展名的非 Python 文件。 “lib-dnyload”中有大量 .so 文件。

通过谷歌搜索,我发现这些文件被称为共享对象,并且是用 C 或 C++ 编写的。我有一台 Mac,并且使用 GCC。如何制作可通过 Python 访问的共享对象文件?主要是如何使用 Mac OS X 与 GCC 制作共享对象。

Looking at Python modules and at code in the "lib-dnyload" directory in the Python framework, I noticed whenever code is creating some kind of GUI or graphic it imports a non-Python file with a .so extension. And there are tons .so files in "lib-dnyload".

From googling things I found that these files are called shared objects and are written in C or C++. I have a Mac and I use GCC. How do I make shared object files that are accessible via Python? Mainly just how to make shared objects with GCC using Mac OS X.

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

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

发布评论

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

评论(5

海的爱人是光 2024-09-04 07:11:21

Python 官方文档从简单的示例开始。对于开始来说已经足够了。

Official python documentation starts with simple example. It's good enough for start.

扭转时空 2024-09-04 07:11:21

在我看来,最简单的方法是使用 Cython。 Cython 将为您生成一些 C 代码,将其编译为可以从 python 加载的“.so”库。这既简单又无痛。我建议您按照教程进行操作,您应该很快就会拥有一个 so 库。

如果您想了解 python C 扩展背后的所有细节,您应该深入研究 Python 文档C 扩展

In my opinion the easiest way is to use Cython. Cython will generate some C code for you, compile it to make a ".so" library that you can load from python. This is easy and painless. I suggest you to follow the tutorial and you should have a so library pretty soon.

If you want to know all the details behind C extensions of python, you should dive in the Python documentation about C extensions.

_失温 2024-09-04 07:11:21

使用 Python 构建和安装 C 扩展模块的标准方法是使用 Distutils 它包含在标准 Python 库中。一般来说,这意味着将 C 文件包含在项目目录中并创建一个配置文件(通常称为 setup.py)来标识 C 源文件。通过在 Python 下运行正确配置的 setup.py 文件,Distutils 将使用系统的 C 编译器和链接器来编译和链接 C 源文件。 (在 OS X 上,您需要安装 Apple 的免费 Xcode 开发人员工具,该工具会安装 gcc 版本。) 仔细阅读所有 Distutils 文档以了解该怎么做现在可能会让人不知所措。这是一个简单的教程,应该足以帮助您入门。

为什么使用 Distutils setup.py 而不是自己调用 gcc?几个原因:

  1. 由于 C 扩展通常在 Python 解释器本身的上下文中运行,因此以与解释器中的 C 代码兼容的方式编译和链接扩展非常重要。 Distutils 试图通过为您正在执行的 Python 提供正确的选项来消除猜测。这在 OS X 上尤其重要,因为 OS X 中的 Python 具有多种风格,其中一些具有多架构可执行文件的各种组合(即 -arch i386-arch ppc-arch x86_64-arch ppc64)并支持多个 OS X 版本(即当前的 python.org OS X 安装程序与 OS X 10.4 到 10.6 兼容)。

  2. Distutils 允许您以独立于机器和操作系统的方式打包和分发纯 Python 和 C 扩展模块的组合。在许多情况下,如果编写 C 代码是为了避免依赖于操作系统的系统调用等,那么您的 C 扩展可以在大多数当前的 Python 平台上构建和安装,而无需修改,并且无需知道要使用哪个 C 编译器或选项

  3. 您会发现几乎所有带有 C 代码的现代 Python 第三方包都以这种方式工作,因此最好从一开始就养成使用 Distutils 的习惯。

  4. 正如您将在本教程中看到的,在许多情况下设置 setup.py 文件非常容易。

The standard way to build and install C extension modules with Python is to use the features of Distutils which is included in the standard Python library. Generally that means including the C file(s) in your project directory and creating a configuration file, normally called setup.py, to identify the C source files. By running a properly-configured setup.py file under Python, Distutils will take care of compiling and linking the C source files by using your system's C compiler and linker. (On OS X, you'll need to have installed Apple's free Xcode Developer Tools which installs versions of gcc.) Wading through all the Distutils documentation to figure out what to do can be overwhelming right now. Here's a simple tutorial that should be enough to get you started.

Why use a Distutils setup.py rather than just calling gcc yourself? Several reasons:

  1. Since C extensions normally run within the context of the Python interpreter itself, it is important that extensions be compiled and linked in a manner compatible with the C code in the interpreter. Distutils tries to take the guesswork out of this by supplying the right options for the Python you are executing under. This can be particularly important on OS X where Pythons come in various flavors including some with various combinations of multi-architecture executables (i.e. -arch i386, -arch ppc, -arch x86_64, -arch ppc64) and with support for multiple OS X versions (i.e. the current python.org OS X installers are compatible with OS X 10.4 through 10.6).

  2. Distutils allows you to package up and distribute combinations of pure Python and C extension modules in a machine and operating system independent way. In many cases, if the C code is written to avoid operating system dependent system calls and the like, your C extensions can be built and installed on most current Python platforms without modification and without you having to know which C compiler to use or options to set.

  3. You'll find that nearly all modern Python third-party packages with C code work this way so it's good to get in the habit of using Distutils for this from the start.

  4. As you'll see in the tutorial, it's very easy to setup a setup.py file for many cases.

赤濁 2024-09-04 07:11:21

您可以通过多种方式编写 python 扩展,包括 Cython、SWIG、boost.python ...
您还可以编写共享库并使用“ctypes”库来访问它。

You can write python extensions in many ways, including Cython, SWIG, boost.python ...
You can also write a shared library and use the "ctypes" library to access it.

悟红尘 2024-09-04 07:11:21

有不同的方法可以做到这一点:

  • 使用 Python.hC 代码中的 a> 标头。这会产生最快的代码。
  • 在 Python 标头周围使用包装器,例如 SWIG。代码可能会慢一些,但您不需要或只需要对 C/C++ 源代码进行很少的更改。在大多数情况下,您只需要编写 SWIG 接口文件,共享对象就会与Python/PHP/Java 等。这是开始包装 C/C++ 代码的最简单方法。
  • 如果您对 Python 语言更熟悉,并且希望避免使用 C/C++ 风格编写 SWIG 接口文件,而不是 Cython是最适合您的包装。

There are different ways to do this:

  • Use the Python.h header in your C code. This produces the fastest code.
  • Use wrappers around the Python headers like SWIG. The code may be a bit slower but you need no or minimal change in the C/C++ source code. In most of case you just need to write a SWIG interface files and the shared objects are integrated with Python/PHP/Java etc. It is the easiest way to start to wrapp your C/C++ code.
  • If you are more familiar with Python language and want to avoid writing SWIG interface files in C/C++ style than Cython is the best wrapper for you.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文