一直在玩cython.通常用Python编程,但在前一生中使用过C. 我无法弄清楚如何制作一个独立的可执行文件.
我已经下载了cython,我可以创建一个.pyx文件(这只是一个扩展名为.pyx的普通Python文件),它在Python shell中执行,使用:import pyximport; pyximport.install()
我可以在命令行生成一个.c文件:cython file.pyx我可以通过构建标准的setup.py来生成一个.so文件并执行:
setup.py build_ext --inplace
Run Code Online (Sandbox Code Playgroud)
我尝试使用带有各种选项的gcc从.so文件中创建一个可执行文件,但总是有大量丢失的文件,标题等.尝试过从几乎所有地方指向标题,但没有成功,并且我并不熟悉所有gcc选项的功能,或者即使我应该使用gcc.
我在这里断开了事实,我可以在Python shell中运行我的程序,但不是在命令行,(我不希望用户必须进入shell,导入模块等).
我在这里错过了什么?
我已阅读在 Cython 中制作可执行文件和 BuvinJ 对如何有效地混淆 Python 代码的回答?并想测试编译后用 Cython 编译的源代码是否真的“不再存在”。确实流行使用 Cython 是一种保护 Python 源代码的方法,例如参见文章Protecting Python Sources With Cython。
让我们以这个简单的例子为例test.pyx:
import json, time # this will allow to see what happens when we import a library
print(json.dumps({'key': 'hello world'}))
time.sleep(3)
print(1/0) # division error!
Run Code Online (Sandbox Code Playgroud)
然后让我们使用 Cython:
cython test.pyx --embed
Run Code Online (Sandbox Code Playgroud)
这会产生一个test.c. 让我们编译它:
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
cl test.c /I C:\Python37\include /link C:\Python37\libs\python37.lib
Run Code Online (Sandbox Code Playgroud)
有用!它生成了一个 140KB 的test.exe可执行文件,不错! …
我正在尝试编译不使用动态加载器的可执行文件(ELF文件)。我曾经Cython将Python编译为C:
cython3 -3 test.py --embed
Run Code Online (Sandbox Code Playgroud)
然后
gcc test.c -otest $(pkg-config --libs --cflags python3)
Run Code Online (Sandbox Code Playgroud)
编译C生成的文件。
现在,我想test.c在静态可执行文件中进行编译。通常我使用-staticflag,但是在这里return collect2: error: ld returned 1 exit status。
我能怎么做?
编辑1: 不是完整的消息,因为正文限制为30000个字符
一长串如下警告和错误:
...
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x430): undefined reference to `XML_SetStartCdataSectionHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x458): undefined reference to `XML_SetEndCdataSectionHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x480): undefined reference to `XML_SetDefaultHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x4a8): undefined reference to `XML_SetDefaultHandlerExpand'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x4d0): undefined reference to `XML_SetNotStandaloneHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x4f8): undefined reference to `XML_SetExternalEntityRefHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x520): undefined reference to `XML_SetStartDoctypeDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x548): undefined reference to `XML_SetEndDoctypeDeclHandler'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpython3.5m.a(pyexpat.o):(.data+0x570): undefined reference to `XML_SetEntityDeclHandler' …Run Code Online (Sandbox Code Playgroud)