简体   繁体   中英

linking C++ and Python using Boost.Python, on Remote Hosting

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!!!

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM