编程语言
首页 > 编程语言> > 连接boost :: python :: numpy时出现问题

连接boost :: python :: numpy时出现问题

作者:互联网

我写了一个小例子来说明发生了什么.

my_test.cpp

#include <iostream>
#include <boost/python/numpy.hpp>
namespace np = boost::python::numpy;
int my_Foo() 
{
    Py_Initialize();
    np::initialize();
    std::cout << "hello\n";
    return 0;
}
BOOST_PYTHON_MODULE(my_test)
{
    using namespace boost::python;
    def("my_Foo", my_Foo);
}

py_test.py

import my_test as t
t.my_Foo();

我用命令编译所有:

g++ -shared -fPIC -o my_test.so my_test.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7

我收到这个错误:

ImportError: /home/my_test.so: undefined symbol: _ZN5boost6python5numpy10initializeEb

它在我评论这一行时起作用

//np::initialize();

我不知道如何解决它.我在论坛上看过类似的问题,但没有一个解决方案对我有帮助.我尝试了更新boost,更新python,链接库,在编译期间将生成的模块放在其他模块之前 – 没有任何帮助.我将不胜感激任何帮助.

解决方法:

在仿生
-lboost_python是不够的.你缺少-lboost_numpy.

在Xenial上,您将找不到预构建的库:

sudo apt -y install libpython2.7-dev libboost-python-dev
git clone https://github.com/ndarray/Boost.NumPy
cd Boost.Numpy
mkdir build
cd build
cmake ..
make 
sudo make install

用boost / numpy.hpp替换你的代码boost / python / numpy.hpp也用命名空间替换命名空间np = boost :: python :: numpy np = boost :: numpy; |

g++ -o test5.so -fPIC -shared test5.cpp -lboost_python -lboost_numpy -I/usr/local/include -I/usr/include/x86_64-linux-gnu/python2.7/ -I/usr/include/python2.7
enter code here

 ~> LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib64 ldd test5.so 
   linux-vdso.so.1 =>  (0x00007ffe9cd36000)
   libboost_python-py27.so.1.58.0 => /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0 (0x00007ffba47bd000)
   libboost_numpy.so => /usr/local/lib64/libboost_numpy.so (0x00007ffba45a2000)
   libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007ffba4216000)
   libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007ffba3ffe000)
   libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ffba3c34000)
   libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ffba3a17000)
   libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007ffba3489000)
   libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ffba3180000)
   /lib64/ld-linux-x86-64.so.2 (0x00007ffba4c11000)
   libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ffba2f66000)
   libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ffba2d62000)
   libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007ffba2b5f000)

标签:python,c,numpy,boost,undefined-symbol
来源: https://codeday.me/bug/20190910/1801529.html