美文网首页
使用Cython的一些小问题

使用Cython的一些小问题

作者: Spooking | 来源:发表于2019-03-20 10:06 被阅读0次

安装:

py -m pip install cython

py代码转C代码

py -m cython main.py --embed

此处如果不加 --embed ,gcc编译时会出现 undefined reference to `wWinMain@16' 错误

如果遇到这样的提示
FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2).

在你的py代码中加入(如果你本身用的就是python2可以忽略)

# cython: language_level=3

编译

#编译为控制台应用,测试期最好使用这种方式,可以知道为啥报错
gcc -o main.exe main.c -I E:\Soft\Python\3.6.8_32\include -L E:\Soft\Python\3.6.8_32\libs -lpython36 -municode

#编译为window视窗程序(无黑框)
gcc -o main.exe main.c -I E:\Soft\Python\3.6.8_32\include -L E:\Soft\Python\3.6.8_32\libs -lpython36 -municode -Wl,--subsystem,windows

调试

编译好的程序运行可能会遇到:

NameError: name '__file__' is not defined

这是因为 __file__ 只针对py文件存在,编译之后是无效的,需要替换成:

os.getcwd()  //运行目录
sys.path[0]  //执行文件所在目录

至于dll丢失,路径不对等问题,慢慢排查吧

相关文章

网友评论

      本文标题:使用Cython的一些小问题

      本文链接:https://www.haomeiwen.com/subject/tdwamqtx.html