【发布时间】:2020-05-30 18:38:36
【问题描述】:
我正在努力学习 Cython,我想从 python 中调用一个简单的 C++ 函数
在构建时,我要么有一个 uio.obj : error LNK2001: unresolved external symbol _just_a_func,要么当我尝试不同的 cython 原型组合时,我的 just_a_func() 函数没有出现在我的模块中。
这里是所有代码,setup.py、test.py、.pxd、.pyx 和 .h
########################
### uio.h
########################
#include <string>
using namespace std;
struct uio{
int i;
uio():i(2){}
float f;
string s;
// float fun(int a);
float fun(int a){return float(a+i);}
};
// int just_a_func(string s);
int just_a_func(string s){return s.length();}
########################
### uio.pxd
########################
from libcpp.string cimport string
cdef extern from "uio.h":
cdef extern int just_a_func(string s)
########################
### uio.pyx
########################
# distutils: language = c++
from uio cimport just_a_func
cdef extern int just_a_func(s):
return just_a_func(s)
########################
### setup.py
########################
# python .\setup.py build_ext --inplace --compiler=msvc
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("uio.pyx"))
########################
### test2.py
########################
import uio
print(dir(uio))
print("just_a_func", uio.just_a_func("fdsfds"))
【问题讨论】:
标签: python c++ cython cythonize