链接有问题 boost::python::numpy

Trouble with linking 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();

我不知道如何修复它。我在论坛上阅读过类似的问题,但 none 的解决方案对我有所帮助。我尝试了更新提升、更新 python、link 库,在编译期间将生成的模块放在其他模块之前 - 没有任何帮助。我将不胜感激。

关于仿生 -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/python/numpy.hpp 替换为 boost/numpy.hpp 并将 namespace np = boost::python::numpy 替换为 namespace 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)

我在链接 python3 或更高版本时也遇到了这个问题。 在那种情况下,有用的是添加这些链接选项:

-lboost_python3 -lboost_numpy3

显然没有找到-lboost_numpy,因此我们需要使用-lboost_numpy3

我不确定你的 python 版本和升级版本。 我的版本: python2.7 boost1.67


如果 cmakeList.txt 是:

link_libraries(${Pangolin_LIBRARIES}
               boost_python
               boost_numpy
               assimp
               util)
所以把它改成
link_libraries(${Pangolin_LIBRARIES}
               boost_python27
               boost_numpy27
               assimp
               util)
因为:

The Boost.Python library names now contain the Python version suffix. A variant compiled with Python 2.7 will thus produce library names boost_python27 and boost_numpy27, etc., making it possible to host variants for multiple Python versions next to each other.(from here) you can check your boost library path.

我设法通过以下命令编译并 link OP 使用 Boost 1.71.0 和 Python 3.5 给出的代码:

g++ -o my_test.so -fPIC -shared my_test.cpp \
-I/usr/include/python3.5  `# for pyconfig.h` \
-L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu  `# for libpython3.5 or libpython3.5m` \
-lpython3.5  \
-I/usr/local/boost_1_71_0/include  `# for Boost header files` \
-L/usr/local/boost_1_71_0/lib  `# for Boost binary files` \
-lboost_python35 -lboost_numpy35

在我的 Linux 系统 (Debian Stretch) 上,Boost 库位于 /usr/local/boost_1_71_0