A.hpp
// A.hpp
#ifndef RECTANGLE_HPP
#define RECTANGLE_HPP
#include <iostream>
#include <vector>
namespace test {
double euclid_dist(const std::vector<double> & a) {
return 3.1415926;
}
template <class T,
double (*dist)(const std::vector<T> &)>
class foo {
public:
foo(std::vector<T> _items) {
items = _items;
}
~foo() {}
void inside() {
std::cout << "hello" << std::endl;
}
void func() {
inside();
}
void prt(std::vector<std::vector<T> > a) {
if(a.size() >= 1) {
std::cout << euclid_dist(a[0]) << std::endl;
}
}
private:
std::vector<T> items;
}; // class foo
} // namespace test
#endif
demo.pyx:
# demo.pyx
# distutils: language = c++
from libcpp.vector cimport vector
cdef extern from "A.hpp" namespace "test":
double euculid_dist(const vector [double] & a)
#def test_euclid_dist(const vector [double] a):
# return euclid_dist(a)
cdef extern from "A.hpp" namespace "test":
cdef cppclass foo [double, euclid_dist]:
foo (vector[double]) except +
void func()
void prt(vector[vector[double]])
cdef class PyFoo:
cdef foo *thisptr # hold a C++ instance which we're wrapping
def __cinit__(self, vec):
self.thisptr = new foo(vec)
def __dealloc__(self):
del self.thisptr
def func(self):
self.thisptr.func()
def prt(self, d):
self.thisptr.prt(d)
setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules = cythonize(Extension(
"demo", # the extesion name
sources=["demo.pyx"], # the Cython source and additional C++ source files
language="c++", # generate and compile C++ code
)))
当我使用python setup.py build_ext --inplace
编译时,出现以下错误:
Compiling demo.pyx because it changed.
Cythonizing demo.pyx
running build_ext
building 'demo' extension
x86_64-pc-linux-gnu-g++ -pthread -fPIC -I/usr/include/python2.7 -c
demo.cpp -o build/temp.linux-x86_64-2.7/rect.o
demo.cpp:551:20: error: ‘euclid_dist’ was not declared in this scope
demo.cpp:551:31: error: template argument 2 is invalid
demo.cpp: In function ‘int __pyx_pf_4rect_5PyFoo___cinit__(__pyx_obj_4rect_PyFoo*, PyObject*)’:
demo.cpp:866:20: error: ‘euclid_dist’ was not declared in this scope
demo.cpp:866:31: error: template argument 2 is invalid
demo.cpp:866:43: error: invalid type in declaration before ‘;’ token
demo.cpp:881:38: error: ‘euclid_dist’ cannot appear in a constant-expression
demo.cpp:881:49: error: template argument 2 is invalid
demo.cpp:881:60: error: cannot convert ‘std::vector<double>’ to ‘int’ in initialization
demo.cpp: In function ‘PyObject* __pyx_pf_4rect_5PyFoo_4func(__pyx_obj_4rect_PyFoo*)’:
demo.cpp:984:26: error: request for member ‘func’ in ‘* __pyx_v_self->__pyx_obj_4rect_PyFoo::thisptr’, which is of non-class type ‘int’
demo.cpp: In function ‘PyObject* __pyx_pf_4rect_5PyFoo_6prt(__pyx_obj_4rect_PyFoo*, PyObject*)’:
demo.cpp:1038:26: error: request for member ‘prt’ in ‘* __pyx_v_self->__pyx_obj_4rect_PyFoo::thisptr’, which is of non-class type ‘int’
error: command 'x86_64-pc-linux-gnu-g++' failed with exit status 1
看起来在作用域中找不到 euclid_dist
函数,但我认为它在全局作用域中。上面的代码有什么问题吗?谢谢!
eculid
而不是euclid
吗?如果是,你拼写一直都是这样吗? - abarnertdouble (*eculid_dist_p)(const std::vector<T> &) = &eculid_dist
,然后从Cython引用它而不是从eculid_dist
,看看是否有效。 - abarnertpyx
文件以从 cpp 文件中导入变量? - xunzhang