简体   繁体   English

使用 cython 和 cythonize 从 python 调用 C++ function

[英]Calling a C++ function from python, using cython, with cythonize

I'm trying to learn Cython, and I want to call a simple C++ function from python我正在尝试学习 Cython,我想从 python 调用一个简单的 C++ function

When building, I either have a uio.obj: error LNK2001: unresolved external symbol _just_a_func , or when I tried different combinations of cython prototypes, my just_a_func() function does not land in my module.构建时,我要么有一个uio.obj: error LNK2001: unresolved external symbol _just_a_func ,要么当我尝试不同的 cython 原型组合时,我的 just_a_func() function 没有登陆我的模块。

Here is all the code, setup.py, test.py, .pxd, .pyx and.h这是所有代码,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"))

May be, the explicit definition your function as "clear C" is helping you.可能是,将您的 function 明确定义为“清除 C”正在帮助您。 In any case, your error message specifies that type of function.在任何情况下,您的错误消息都会指定 function 的类型。 Try this:尝试这个:

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

Found a solution on https://dmtn-013.lsst.io/https://dmtn-013.lsst.io/上找到了解决方案

This is the working solution这是工作解决方案

########################
### 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())))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM