在 Rafael 的回答基础上,这里提供了使用 Cython 和 Python 3(在 Termux 上测试过)创建“Hello, World”可执行文件的说明,以及 Python 2 的说明:
由于我的系统上似乎没有“cython3”选项,所以我做了以下操作:
文件名为 test.py:
print("Hello, world!")
然后执行这个操作:
cython -3 --embed -o test.c test.py
然后你需要弄清楚你需要什么包含路径,以及可能需要的库。
我正在使用Python 3.10.6;因此,我使用一个名为python3.10-config的程序来获取信息,然后将其输入到gcc中:
python3.10-config --includes
对我来说,这将输出:
-I/data/data/com.termux/files/usr/include/python3.10 -I/data/data/com.termux/files/usr/include/python3.10
它给了我两次相同的路径(我不知道为什么),所以只需使用该路径(我想你只需要使用一次)。
然后执行
python3.10-config --libs
对我来说,这将输出:
-lpython3.10 -lcrypt -ldl -lm -lm
我不确定每个程序都需要这些,因为只要我加上-lpython3.10
,'Hello, world'就可以工作。
无论如何,然后我这样做,它会给我一个可执行文件:
gcc -Os -I/data/data/com.termux/files/usr/include/python3.10 test.c -lpython3.10 -o test
还有一个叫做python2.7-config的程序。
如果我想要获得一个Python 2.7.18可执行文件,可以使用以下命令代替上面的命令:
cython -2 --embed -o test.c test.py
那么
python2.7-config --includes
python2.7-config --libs
这给了我这些:
-I/data/data/com.termux/files/usr/include/python2.7 -I/data/data/com.termux/files/usr/include/python2.7
-lpython2.7 -ldl -lm
最后,我运行以下命令以生成可执行文件:
gcc -Os -I/data/data/com.termux/files/usr/include/python2.7 test.c -lpython2.7 -o test