简体   繁体   English

链接Boost.python示例的问题

[英]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 . 我正在尝试编译示例,但收到有关undefined reference to PyInt_Type/PyString_FromString/PyNumber_Divide等的undefined reference to PyInt_Type/PyString_FromString/PyNumber_Divide 。我已经将构建链接到boost_pythonpython3.6m

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

main.cpp 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 我已经安装了python3.6-dev
  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 我正在使用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 . 另外 :如果我理解正确的话以后的事发生了:当我和lboost_python它使用了一些功能链接我的生成PyInt_TypePyString_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. 它具有关于它们的返回类型和输入参数的信息,但是没有关于它们的实际定义(即函数体)的信息,这些信息是在其他库中定义的(在我的情况下是python库),我必须告诉链接程序有关此库的信息,以将其包含在其中建立。 Is my understanding correct ? 我的理解正确吗? If yes then why linking against python3.6m didn't help ? 如果是,那为什么对python3.6m链接没有帮助?

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). boost_python可能指向python 2.7版本,例如在debian Stretch中(应该具有与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. 我猜该库的python 3.6版本可能尚不可用。 The easiest option may be to use python 3.5 if libboost_python-py35 is not compatible with python 3.6, ie 如果libboost_python-py35与python 3.6不兼容,则最简单的选择是使用python 3.5,即

-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 . 因为boost_python依赖于python3.6m ,所以boost_python必须排在最前面,而python3.6m必须boost_python python3.6m

PS: This is wrong: -L/usr/include : -L tells the linker where to find libraries, but /usr/include contains headers, not libraries. PS:这是错误的: -L/usr/include-L告诉链接器在哪里可以找到库,但是/usr/include包含标头,而不是库。 You should not need it at all, but if you do, it is -L/usr/lib or similar. 您根本不需要它,但如果需要,它是-L/usr/lib或类似名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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