在Cython中制作可执行文件 [英] Making an executable in Cython

查看:242
本文介绍了在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标志. 上没有很多文档,但是是我能够做到的找.它确实链接到一个简单的工作示例.

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.solibpython27.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命令将是

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命令选项指南可能会有所帮助.

This gcc command option guide may be helpful.

此外,如果可能的话,我建议您使用Cython内存视图. 这样可以避免您在Cython文件中包含NumPy标头并将NumPy pxd文件包含在内. 这也使切片操作更易于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中制作可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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