链接C++和Python使用boost.python,在远程托管上
首先, 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中进行以下操作:
- 使用C ++代码创建Hello.cpp文件并将其上传。
- 运行以下两个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:
- Create hello.cpp file with C++ code and upload it.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
-lboost_python
您应该使用-lboost_python38
,因为您正在使用Python3.8。Instead of
-lboost_python
you should use-lboost_python38
, since you are using python3.8.