使用Cython的一些小问题

安装:

py -m pip install cython

py代码转C代码

py -m cython main.py --embed

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

若是遇到这样的提示windows

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

调试

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

NameError: name '__file__' is not defined.net

这是由于 __file__ 只针对py文件存在,编译以后是无效的,须要替换成:调试

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

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



做者:Spooking
连接:https://www.jianshu.com/p/d2e82fd0f76d
 ip

相关文章
相关标签/搜索