编写一个c ++程序并尝试使用 Boost.Python 使其与python兼容。在Eclipse中运行c ++程序运行正常。但是,当我从该c ++程序创建一个共享库(vectorEg.so)并尝试在python中导入它时,它显示一个错误。
In [1]: import vectorEg
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-fd697b4a9962> in <module>()
----> 1 import vectorEg
ImportError: ./vectorEg.so: undefined symbol: _ZN4itppplIdEENS_3VecIT_EERKS3_S5_
C ++代码:
#include <itpp/itbase.h>
#include <boost/python.hpp>
using namespace itpp;
vec testFunc()
{
//Declare vectors and matricies:
vec a, b, c;
a = linspace(1.0, 2.0, 10);
//Use a string of values to define a vector:
b = "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0";
//Add two vectors:
c = a + b;
return c;
}
BOOST_PYTHON_MODULE(vectorEg)
{
using namespace boost::python;
using namespace itpp;
def("testFunc", testFunc);
}
还尝试使用itpp :: Vec作为矢量声明。
#include <itpp/itbase.h>
#include <boost/python.hpp>
using namespace itpp;
itpp::Vec<double> testFunc()
{
//Declare vectors and matricies:
itpp::Vec<double> a, b, c;
a = linspace(1.0, 2.0, 10);
//Use a string of values to define a vector:
b = "0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0";
//Add two vectors:
c = a + b;
return c;
}
BOOST_PYTHON_MODULE(vectorEg)
{
using namespace boost::python;
using namespace itpp;
def("testFunc", testFunc);
}
对于共享库的编译和创建,我正在使用这段代码
g++ -c vectorEg.cpp -litpp -o vectorEg.o -fPIC -I/usr/include/python2.7/
g++ -shared vectorEg.o -lboost_python -lpython2.7 -o vectorEg.so
sudo cp vectorEg.so /usr/lib/python2.7/dist-packages # adding file to python path.
另外我安装了IT ++
sudo apt-get install libitpp-dev # for ubuntu 16.0.4