[英]Basic C++ function to Python via Cython
I have a very basic setup to experiment on how to expose a C++ function to Python via Cython.我有一个非常基本的设置来试验如何通过 Cython 向 Python 公开 C++ 函数。 However, I keep getting the following error.
但是,我不断收到以下错误。 What am I missing?
我错过了什么?
foo.obj : error LNK2001: unresolved external symbol cppFoo
C:\git\cythonTest\foo.cp38-win_amd64.pyd : fatal error LNK1120: 1 unresolved externals
cppFoo.h
#ifndef FOO_H
#define FOO_H
double cppFoo(double x, int y);
#endif
cppFoo.cpp
#include "cppFoo.h"
double cppFoo(double x, int y)
{
return 2 * x + y;
}
cfoo.pxd
cdef extern from "cppFoo.h":
double cppFoo(double x, int y)
foo.pyx
from cfoo cimport cppFoo
def pyFoo(double x, int y):
return cppFoo(x, y)
setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("foo.pyx"), requires=['Cython'])
Running with python setup.py clean build_ext --inplace
.使用
python setup.py clean build_ext --inplace
运行。
I found a solution.我找到了解决方案。
cppFoo.cpp
was not being recognized as a source file. cppFoo.cpp
未被识别为源文件。 Adding the following line at the top of foo.pyx
was enough.在
foo.pyx
的顶部添加以下行就足够了。
# distutils: sources = cppFoo.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.