I wrote a small example to show what's going on.
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();
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. I will be grateful for any help.
On Bionic -lboost_python
is not enough. You are missing -lboost_numpy
.
On Xenial you won't find prebuilt libraries yet:
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
; |
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. 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
I managed to compile and link the code given by OP with Boost 1.71.0 and Python 3.5 via the following command:
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
.
i am not sure your python version and boost version. my version: python2.7
boost1.67
If the cmakeList.txt
is:
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.