使用Boost 1.63.0,我编写了以下代码:
vectors.cpp
/* Boost/Python headers */
#include<boost/python/module.hpp>
#include<boost/python/def.hpp>
#include<boost/python/extract.hpp>
#include<boost/python/numpy.hpp>
#include<cmath>
using namespace boost::python;
namespace np = boost::python::numpy;
double eucnorm(np::ndarray axis){
const int n = axis.shape(0);
double norm = 0.0;
for(int i = 0; i < n; i++){
double A = boost::python::extract<double>(axis[i]);
norm += A*A;
}
return sqrt(norm);
}
BOOST_PYTHON_MODULE(vectors){
def("eucnorm", eucnorm);
}
我用以下方法编译了这个:
g++ -shared -fpic -I /usr/include/python2.7 -I /foo/bar/boost_1_63_0 -lboost_python -o vectors.so
导入时出现以下错误:
from vectors import *
ImportError: ./vectors.so: undefined symbol: _ZN5boost6python9converter21object_manager_traitsINS0_5numpy7ndarrayEE10get_pytypeEv
这是什么意思,我该如何解决这个问题?
答案 0 :(得分:3)
添加:
-lboost_numpy -lboost_python
构建.so。
时顺便说一句,如果你想在构建时发现这些问题(而不是在Python中尝试import
):Force GCC to notify about undefined references in shared libraries