我正在尝试用Cython包装模板化的c ++函数。我有以下文件用于实现:
max.cpp
//max.cpp
template <class T>
const T& cppmax(const T& a, const T& b) {
return a < b ? b : a;
}
最大小时
//max.h
ifndef MAX_H
#define MAX_H
template <class T>
const T& cppmax(const T& a, const T& b);
#endif
Driver.pyx
# externs
cdef extern from 'max.h':
const T cppmax[T] (T a, T b)
def cmax(double a, double b):
return cppmax(a,b)
test_extern.py
import Driver
if __name__ == '__main__':
print(Driver.cmax(0.00001, 0.02))
我正在设置:max_setup.py
from setuptools import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules = [
Extension('Driver', sources=['Driver.pyx', 'max.cpp'], language='c++'),
]
setup(
name='Driver',
ext_modules=cythonize(ext_modules, compiler_directives={'language_level' : '3'})
)
我使用标准python max_setup.py build_ext --inplace
进行编译,然后运行python test_extern.py
。在此之后,出现以下错误:
Traceback (most recent call last):
File "test_extern.py", line 1, in <module>
import Driver
ImportError: /home/me/project/Driver.cpython-37m-x86_64-linux-gnu.so: undefined symbol: _Z6cppmaxIdERKT_S2_S2_
我不清楚为什么会出现此错误。我将不胜感激,可以提供有关此错误发生原因以及如何解决的任何见解。谢谢!