使用 Cython,您可以使用 cdef 关键字(和 public... 重要!),使用 Python 内部代码编写声明为 C 的函数:
yourext.pyx
cdef int public func1(unsigned long l, float f):
print(f) # some python code
注意:以下假设我们工作在驱动器 D 的根目录:\
建筑 (setup.py)
from distutils.core import setup
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
name = 'My app',
ext_modules = cythonize("yourext.pyx"),
)
然后运行python setup.py build_ext --inplace
运行setup.py(如果你使用distutils)后,你会得到2个感兴趣的文件:
查看.c 最终会告诉您func1 是一个C 函数。
剩下的就是这两个文件了。
用于测试的 C 主程序
// test.c
#include "Python.h"
#include "yourext.h"
main()
{
Py_Initialize(); // start python interpreter
inityourext(); // run module yourext
func1(12, 3.0); // Lets use shared library...
Py_Finalize();
}
由于我们不单独使用扩展名 (.pyd),我们需要在头文件中做一些小技巧/破解来禁用“DLL 行为”。在“yourext.h”开头添加以下内容:
#undef DL_IMPORT # Undefines DL_IMPORT macro
#define DL_IMPORT(t) t # Redefines it to do nothing...
__PYX_EXTERN_C DL_IMPORT(int) func1(unsigned long, float);
将“yourext”编译为共享库
gcc -shared yourext.c -IC:\Python27\include -LC:\Python27\libs -lpython27 -o libyourext.dll
然后编译我们的测试程序(链接到DLL)
gcc test.c -IC:\Python27\include -LC:\Python27\libs -LD:\ -lpython27 -lyourext -o test.exe
最后,运行程序
$ test
3.0
这并不明显,还有很多其他方法可以实现相同的目标,但这是可行的(查看boost::python,...,其他解决方案可能更适合您的需求)。
我希望这能回答你的问题,或者至少给你一个想法......