在 Cython 中制作可执行文件

时间:2023-04-17
本文介绍了在 Cython 中制作可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

一直在玩 cython.通常使用 Python 编程,但在前世使用过 C.我不知道如何制作独立的可执行文件.

Been playing with cython. Normally program in Python, but used C in a previous life. I can't figure out how to make a free-standing executable.

我已经下载了 cython,我可以制作一个 .pyx 文件(这只是一个带有 .pyx 扩展名的普通 Python 文件),它在 Python shell 中执行,使用:导入pyximport;pyximport.install()

I've downloaded cython, and I can make a .pyx file (that's just a normal Python file with a .pyx extension), that executes in the Python shell, using: import pyximport; pyximport.install()

我可以在命令行生成一个 .c 文件:cython file.pyx我可以通过构建标准 setup.py 并执行来生成 .so 文件:

I can generate a .c file at the command line with: cython file.pyx I can generate a .so file by building a standard setup.py and executing:

setup.py build_ext --inplace

我尝试使用带有各种选项的 gcc 从 .so 文件中制作可执行文件,但总是有大量丢失的文件、头文件等.我尝试从几乎所有地方指向头文件,但没有成功,现在不太熟悉所有 gcc 选项的作用,或者即使我应该使用 gcc.

I've tried making an executable out of the .so file using gcc with various options, but always have tons of missing files, headers, etc. Have tried pointing to headers from virtually everywhere, but with no success, and am not really familiar with what all the gcc options do, or even if I should be using gcc.

我在这里与我可以在 Python shell 中运行我的程序,但不能在命令行中运行我的程序,(我不希望用户必须进入 shell,导入模块等).

I've having a disconnect here with the fact that I can run my program in the Python shell, but not at the command line, (I don't want users to have to get into the shell, import modules, etc).

我在这里错过了什么?

推荐答案

你想要的是 Cython 编译器的 --embed 标志.没有大量文档,但 this 是我能够做到的寻找.它确实链接到一个简单的工作示例.

What you want is the --embed flag for the Cython compiler. There isn't a ton of documentation on it, but this is what I was able to find. It does link to a simple working example.

要将 Cython 源代码编译为 C 文件,然后可以编译为可执行文件,您可以使用 cython myfile.pyx --embed 之类的命令,然后使用您使用的任何 C 编译器进行编译.

To compile the Cython source code to a C file that can then be compiled to an executable you use a command like cython myfile.pyx --embed and then compile with whichever C compiler you are using.

当您编译 C 源代码时,您仍然需要包含包含 Python 头文件的目录并链接到系统上相应的 Python 共享库(一个类似 libpython27.so 的文件或 libpython27.a 如果您使用的是 Python 2.7).

When you compile the C source code, you will still need to include the directory with the Python headers and link to the corresponding Python shared library on your system (a file named something like libpython27.so or libpython27.a if you are using Python 2.7).

以下是有关如何获取包含正确标头和链接到正确库的命令的更多说明.

Here are some more instructions on how to get the commands for including the proper headers and linking against the proper libraries.

正如我之前所说,您需要像这样运行 Cython 编译器:

As I said earlier, you need to run the Cython compiler like this:

cython <cython_file> --embed

要使用 gcc 进行编译,您需要找到系统中 python 头文件的位置(您可以通过运行 distutils.sysconfig.get_python_inc() 获取此位置(您必须导入首先).它可能只是 Python 安装目录中的 /include 子目录.

To compile using gcc, you will need to find where the python headers are on your system (you can get this location by running distutils.sysconfig.get_python_inc() (you'll have to import it first). It is probably just the /include subdirectory in your Python installation directory.

您还必须找到 python 共享库.对于 Python 2.7,它将是 Windows 上的 libpython27.a 或 Linux 上的 libpython2.7.so.

You will also have to find the python shared library. For Python 2.7 it would be libpython27.a on Windows or libpython2.7.so on Linux.

你的 gcc 命令将是

Your gcc command will then be

gcc <C_file_from_cython> -I<include_directory> -L<directory_containing_libpython> -l<name_of_libpython_without_lib_on_the_front> -o <output_file_name>

包含 -fPIC 标志可能是明智的.在 Windows 64 位机器上,您还必须包含标志 -D MS_WIN64 告诉 mingw 为 64 位窗口编译.

It may be wise to include the -fPIC flag. On Windows 64 bit machines you will also have to include the flags -D MS_WIN64 that tells mingw to compile for 64 bit windows.

如果您正在编译依赖于 NumPy 的内容,您还需要包含包含 NumPy 标头的目录.您可以通过运行 numpy.get_include() 找到此文件夹(同样,在导入 numpy 之后).然后你的 gcc 命令就变成了

If you are compiling something that depends on NumPy, you will also need to include the directory containing the NumPy headers. You can find this folder by running numpy.get_include() (again, after importing numpy). Your gcc command then becomes

gcc <C_file_from_cython> -I<include_directory> -I<numpy_include_directory> -L<directory_containing_libpython> -l<name_of_libpython_without_lib_on_the_front> -o <output_file_name>

这个 gcc 命令选项 guide 可能会有所帮助.

This gcc command option guide may be helpful.

另外,如果可能,我建议您使用 Cython 内存视图.这样您就不必包含 NumPy 标头并将 NumPy pxd 文件包含在 Cython 文件中.它还使 C 编译器更容易优化切片操作.

Also, I would recommend you use Cython memory views if possible. That will make it so that you won't have to include the NumPy headers and include the NumPy pxd file in your Cython file. It also makes slicing operations easier for the C compiler to optimize.

这篇关于在 Cython 中制作可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在 Mac 上安装 python 模块最兼容的方式是什么? 下一篇:如何编写好的/正确的包 __init__.py 文件

相关文章