链接C++和Python使用boost.python,在远程托管上

发布于 2025-01-21 18:38:17 字数 1650 浏览 3 评论 0原文

首先, pythonanywhere 是一个了不起的远程托管网站,如果不只是<,那就太好了代码> python 。

我已经阅读了大量解决方案,试图获得一个简单的示例可以使用,以尝试使用c ++代码,使用python pythonanywhere

我的帐户中的C ++代码

char const* greet()
{
   return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

在pythonany的Where中进行以下操作:

  1. 使用C ++代码创建Hello.cpp文件并将其上传。
  2. 运行以下两个CMD:

GCC -O中间-C -FPIC -I/usr/include/python3.8/ -l/usr/include/python3.8/ hello.cpp

gcc -o hello.so -shared中间-lboost_python -lpython3.8

第二个命令意外地终止错误:

/usr/bin/bin/ld:找不到-lboost_python

如果我运行第二个命令没有-lboost_python参数,它会成功,但是当我使用python时,会抛出错误。

$ python3.8
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/Ottoman/hello.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

我需要两种答案。首先,我在做什么错,如何解决问题?其次,我想做的是好练习?因为我的最终目标是执行更复杂的C ++代码。如果没有,我有什么选择?谢谢你!!!

First, pythonanywhere is an amazing remote hosting site, and it would be great if it weren't just for python.

I've read loads of solutions, trying to get this simple example to work, in an attempt to use c++ code, using python code hosted in pythonanywhere.

c++ code

char const* greet()
{
   return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

In my account in pythonanywhere I did the following:

  1. Create hello.cpp file with C++ code and upload it.
  2. Run the following two cmds:

gcc -o middle -c -fPIC -I /usr/include/python3.8/ -L /usr/include/python3.8/ hello.cpp

gcc -o hello.so -shared middle -lboost_python -lpython3.8

The second command terminates unexpectedly showing the error:

/usr/bin/ld: cannot find -lboost_python

If I run the second command without the -lboost_python argument, it succeeds, but when I use python an error is thrown.

$ python3.8
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/Ottoman/hello.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

I need two kinds of answers. First, what am I doing wrong and how can I solve my problem? Second, What I'm trying to do is good practice?, since my ultimate goal is to execute much more complex C++ code. If not, what alternatives do I have? Thank you!!!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2025-01-28 18:38:17

而不是-lboost_python您应该使用-lboost_python38,因为您正在使用Python3.8。

Instead of -lboost_python you should use -lboost_python38, since you are using python3.8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文