我对Cython并不陌生,对c ++也不太了解,并且正在尝试获取包装其参数通过引用传递的c ++函数的简单示例。这是我的test.h
#ifndef TEST
#define TEST
int add(int& a, int& b);
#endif
我的test.cpp:
#include "test.h"
int add(int& a, int& b)
{
return a + b;
}
我的test.pyx
:
#cython: language_level=3
cdef extern from "test.h":
int add(int&, int&)
cpdef add_em(a, b):
print(add(a, b))
还有我的setup.py
try:
from setuptools import setup
from setuptools import Extension
from Cython.Distutils import build_ext
except ImportError:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules = [Extension(
"test",
sources=["test.pyx"],
language="c++", # this causes Cython to create C++ source
)]
setup(
name='test',
cmdclass={'build_ext': build_ext},
ext_modules=ext_modules
)
然后我用python setup.py build_ext --inplace
进行编译,该编译似乎没有问题
running build_ext
cythoning test.pyx to test.cpp
building 'test' extension
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/thestrangequark/Documents/temp/cython/venv/include -I/Users/thestrangequark/.pyenv/versions/3.6.2/Python.framework/Versions/3.6/include/python3.6m -c test.cpp -o build/temp.macosx-10.13-x86_64-3.6/test.o
clang++ -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/openssl/lib -L/Users/thestrangequark/.pyenv/versions/3.6.2/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/openssl/lib -L/Users/thestrangequark/.pyenv/versions/3.6.2/lib build/temp.macosx-10.13-x86_64-3.6/test.o -o /Users/thestrangequark/Documents/temp/cython/test.cpython-36m-darwin.so
但是为什么我尝试从python import test
收到错误消息
Python 3.6.2 (default, Feb 11 2019, 09:37:08)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/thestrangequark/Documents/temp/cython/test.cpython-36m-darwin.so, 2): Symbol not found: __Z3addii
Referenced from: /Users/thestrangequark/Documents/temp/cython/test.cpython-36m-darwin.so
Expected in: flat namespace
in /Users/thestrangequark/Documents/temp/cython/test.cpython-36m-darwin.so
我对此错误和找不到什么符号感到困惑。我在这里做什么错了?