0

I was following a starter example on how to access a C++ class from Python using boost. (I'm currently working on Ubuntu 21.) I created the file foo.cpp:

// foo.cpp
#include <boost/python.hpp>

class A {

 public:

  A(int i)
      : m_i{i} { }

  int get_i() const {
    return m_i;
  }
 private:
  // don't use names such as `_i`; those are reserved for the
  // implementation
  int m_i;
};

BOOST_PYTHON_MODULE(foo) {
  using namespace boost::python;

  class_<A>("A", init<int>())
      .def("get_i", &A::get_i, "This is the docstring for A::get_i")
      ;
}

I then compiled with the command (I'm using Python 3.9 on my system):

g++ -o foo.so foo.cpp -std=c++11 -fPIC -shared \
-Wall -Wextra `python3.9-config --includes --libs` \
-lboost_python

However, the compile fails with:

In file included from /usr/include/boost/smart_ptr/detail/sp_thread_sleep.hpp:22,
                 from /usr/include/boost/smart_ptr/detail/yield_k.hpp:23,
                 from /usr/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp:14,
                 from /usr/include/boost/smart_ptr/detail/spinlock.hpp:42,
                 from /usr/include/boost/smart_ptr/detail/spinlock_pool.hpp:25,
                 from /usr/include/boost/smart_ptr/shared_ptr.hpp:29,
                 from /usr/include/boost/shared_ptr.hpp:17,
                 from /usr/include/boost/python/converter/shared_ptr_to_python.hpp:12,
                 from /usr/include/boost/python/converter/arg_to_python.hpp:15,
                 from /usr/include/boost/python/call.hpp:15,
                 from /usr/include/boost/python/object_core.hpp:14,
                 from /usr/include/boost/python/args.hpp:22,
                 from /usr/include/boost/python.hpp:11,
                 from foo.cpp:2:
/usr/include/boost/bind.hpp:36:1: note: ‘#pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.’
   36 | BOOST_PRAGMA_MESSAGE(
      | ^~~~~~~~~~~~~~~~~~~~
/usr/include/boost/detail/iterator.hpp:13:1: note: ‘#pragma message: This header is deprecated. Use <iterator> instead.’
   13 | BOOST_HEADER_DEPRECATED("<iterator>")
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: cannot find -lboost_python
collect2: error: ld returned 1 exit status

There are some notes there, but the error seems to be from the inability to find -lboost_python (or -lboost_python3).

I used a guide to check if boost is installed on my system and it is. I then read a guide about finding -lboost_python which told me to try and make a symbolic link between libboost_python.so and the shared library in /usr/lib/. I checked /usr/lib and found /usr/lib/x86_64-linux-gnu/libboost_python39.so and tried to make a symbolic link with:

sudo ln -s libboost_python39.so libboost_python.so

but get the error message that:

ln: failed to create symbolic link 'libboost_python.so': File exists

Which I assume means that the appropriate link already exists (but I'm not sure).

I've been looking around trying to find more guidance on what to do, but I'm stuck now. Does anyone have any insight has to why my system can't find -lboost_python? Also does anyone know if the notes that were thrown during my attempt to compile are issues that need to be corrected, or are they just warnings?

Any help would be greatly appreciated!

matthews
  • 335
  • 1
  • 2
  • 5

0 Answers0