Boost Python 示例:找不到“-lboost_python”

发布于 2025-01-13 06:16:37 字数 3484 浏览 2 评论 0原文

我正在关注入门示例 关于如何使用 boost 从 Python 访问 C++ 类。 (我目前正在使用 Ubuntu 21。)我创建了文件 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")
      ;
}

然后使用该命令进行编译(我在我的系统上使用 Python 3.9):

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

但是,编译失败并显示:

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

那里有一些注释,但错误似乎是由于无法找到 -lboost_python (或 -lboost_python3)。

我使用指南来检查我的系统上是否安装了boost,并且它是。然后我阅读了关于查找<的指南 code>-lboost_python 它告诉我尝试在 libboost_python.so/usr/lib/ 中的共享库之间建立符号链接。我检查了 /usr/lib 并找到了 /usr/lib/x86_64-linux-gnu/libboost_python39.so 并尝试使用以下命令建立符号链接:

sudo ln -s libboost_python39.so libboost_python.so

但收到错误消息那:

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

我认为这意味着适当的链接已经存在(但我不确定)。

我一直在环顾四周,试图找到更多关于该怎么做的指导,但现在我陷入了困境。有谁知道为什么我的系统找不到 -lboost_python ?还有人知道我尝试编译期间抛出的注释是否是需要纠正的问题,或者它们只是警告?

任何帮助将不胜感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文