【问题标题】:Calling a C++ function from python, using cython, with cythonize使用 cython 和 cythonize 从 python 调用 C++ 函数
【发布时间】: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


    【解决方案1】:

    可能是,将您的函数明确定义为“clear C”对您有所帮助。在任何情况下,您的错误消息都会指定该类型的函数。试试这个:

    extern "C" {
      int just_a_func(string s){return s.length();}
    }
    
    

    【讨论】:

      【解决方案2】:

      在https://dmtn-013.lsst.io/找到解决方案

      这是可行的解决方案

      ########################
      ### uio.h
      ########################
      #include <string>
      using namespace std;
      
      int just_a_func(string s){return s.length();}
      
      
      ########################
      ### uio.pxd
      ########################
      from libcpp.string cimport string
      cdef extern from "uio.h":
          int just_a_func(string s)
      
      
      ########################
      ### uio.pyx
      ########################
      # distutils: language = c++
      # note the "as", this is a trick to avoid name clash, to allow the same function name
      from uio cimport just_a_func as cjust_a_func
      
      def just_a_func(s):
          return cjust_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
      from sys import argv
      print(dir(uio))
      print("just_a_func", uio.just_a_func((argv[1].encode())))
      

      【讨论】:

        猜你喜欢
        • 2012-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        相关资源
        最近更新 更多