简体   繁体   English

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

[英]Trouble with linking boost::python::numpy

I wrote a small example to show what's going on.我写了一个小例子来说明发生了什么。

my_test.cpp 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 py_test.py

import my_test as t
t.my_Foo();

I compile all with command :我用命令编译所有:

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

And I get this error :我收到此错误:

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

And it works when I comment this line当我评论这一行时它会起作用

//np::initialize();

I have no idea how to fix it.我不知道如何解决它。 I have read similar questions on the forum, but none of the solutions helped me.我在论坛上读过类似的问题,但没有一个解决方案对我有帮助。 I tried update boost, update python, link libraries, put generated module before other module during compilation - nothing helps.我尝试更新提升,更新 python,链接库,在编译期间将生成的模块放在其他模块之前 - 没有任何帮助。 I will be grateful for any help.我将不胜感激任何帮助。

On Bionic -lboost_python is not enough.在仿生上-lboost_python是不够的。 You are missing -lboost_numpy .您缺少-lboost_numpy

On Xenial you won't find prebuilt libraries yet:在 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

replace in your code boost/python/numpy.hpp with boost/numpy.hpp also replace namespace np = boost::python::numpy with namespace np = boost::numpy ;boost/numpy.hpp替换你的代码boost/python/numpy.hpp也用namespace np = boost::python::numpy替换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)

I also encountered this problem when linking for python3 or above.我在链接python3或更高版本时也遇到了这个问题。 In that case what helped is to add those linking options:在这种情况下,有帮助的是添加这些链接选项:

-lboost_python3 -lboost_numpy3

Apparently -lboost_numpy is not found, thus we need to use -lboost_numpy3显然-lboost_numpy没有找到,因此我们需要使用-lboost_numpy3

I managed to compile and link the code given by OP with Boost 1.71.0 and Python 3.5 via the following command:我设法通过以下命令编译和链接 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

On my Linux system (Debian Stretch), the Boost library is placed in /usr/local/boost_1_71_0 .在我的 Linux 系统 (Debian Stretch) 上,Boost 库位于/usr/local/boost_1_71_0

i am not sure your python version and boost version.我不确定你的 python 版本和 boost 版本。 my version: python2.7 boost1.67我的版本: python2.7 boost1.67


If the cmakeList.txt is:如果cmakeList.txt是:

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

The Boost.Python library names now contain the Python version suffix. Boost.Python 库名称现在包含 Python 版本后缀。 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.因此,使用 Python 2.7 编译的变体将生成库名称 boost_python27 和 boost_numpy27 等,从而可以将多个 Python 版本的变体彼此相邻托管。( 从此处)您可以检查您的 boost 库路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM