链接Boost.python示例的问题

时间:2018-02-10 07:54:09

标签: python c++ boost

我尝试编译this示例,但收到有关undefined reference to PyInt_Type/PyString_FromString/PyNumber_Divide等的错误。我已将我的版本与boost_pythonpython3.6m相关联。

我正在建设它 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);
}

注意

  1. 我安装了python3.6-dev
  2. 我能够使用相同的构建参数和包含指令
  3. 来运行this
  4. 错误的完整list
  5. 我使用的是Ubuntu 16.04
  6. :如果我理解了正在发生的事情:当我将我的构建与lboost_python链接时,它会使用一些函数PyInt_TypePyString_FromString。它有关于它们的返回类型和输入参数的信息,但不是它们的真实定义,即函数体,它在一些其他库中定义(在我的情况下它是python库),我必须告诉链接器这个库要包含它建立。我的理解是否正确?如果是,那么为什么链接python3.6m没有帮助?

2 个答案:

答案 0 :(得分:1)

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

我猜那个库的python 3.6版本可能不容易获得。如果libboost_python-py35与python 3.6不兼容,最简单的选择可能是使用python 3.5,即

-lboost_python-py35

用于动态链接。

答案 1 :(得分:0)

您以错误的顺序链接库。必须先boost_pythonpython3.6m必须在boost_python之后,因为python3.6m取决于-L/usr/include

P.S。:这是错误的:-L/usr/include告诉链接器在哪里找到库,但-L/usr/lib包含头文件,而不是库。你根本不需要它,但是如果你这样做,那就是<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>3.9.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.9.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>3.9.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>3.9.1</version> </dependency> </dependencies> 或类似的。

相关问题