简体   繁体   中英

Issues linking Boost.python example

I'm trying to compile this example but getting errors about undefined reference to PyInt_Type/PyString_FromString/PyNumber_Divide etc. I have already linked my build against boost_python and python3.6m .

I'm building it with g++ example.cpp -L/usr/include/boost/python -lboost_python -lpython3.6m -I/usr/include/python3.6m

main.cpp

#include <boost/python.hpp>
#include <boost/python/detail/wrap_python.hpp>
#include <boost/python/exec.hpp>
#include <boost/python.hpp>
#include <iostream>
#include <string>
#include <Python.h>

using namespace boost::python;

int main() {

  Py_Initialize();
  object main_module = import("__main__");
  object main_namespace = main_module.attr("__dict__");

  object ignored = exec("hello = file('hello.txt', 'w')\n"
                        "hello.write('Hello world!')\n"
                        "hello.close()",
                        main_namespace);
}

Note :

  1. I have python3.6-dev installed
  2. I was able to run this using the same build parameters and include directives
  3. Full list of errors
  4. I'm using Ubuntu 16.04

Also : If i understand correctly following thing happening: When i'm linking my build with lboost_python it uses some functions PyInt_Type , PyString_FromString . It has information about their return types and input parameters, but not their real definitions ie function body, that is defined in some other library (In my case it is python library) and i have to tell linker about this library to be include it in build. Is my understanding correct ? If yes then why linking against python3.6m didn't help ?

boost_python probably points to python 2.7 version, for example in debian stretch (which should have a similar package as in ubuntu 16.04).

> cd /usr/lib/x86_64-linux-gnu
> ls -l libboost_python*.*
... libboost_python-py35.a
... libboost_python-py27.a
... libboost_python.a -> libboost_python-py27.a
... libboost_python-py27.so.1.55.0
... libboost_python-py27.so -> libboost_python-py27.so.1.62.0
... libboost_python.so -> libboost_python-py27.so
... libboost_python-py35.so.1.62.0
... libboost_python-py35.so -> libboost_python-py35.so.1.62.0

I guess python 3.6 version of that library is probably not readily available. The easiest option may be to use python 3.5 if libboost_python-py35 is not compatible with python 3.6, ie

-lboost_python-py35

for dynamic linking.

You're linking the libraries in the wrong order. boost_python must come first, and python3.6m must come after it, because boost_python depends on python3.6m .

PS: This is wrong: -L/usr/include : -L tells the linker where to find libraries, but /usr/include contains headers, not libraries. You should not need it at all, but if you do, it is -L/usr/lib or similar.

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