問題描述
一直在玩 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 中制作可執行文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!