需要帮助入门Boost.Python的 [英] Need help getting started with Boost.Python

查看:125
本文介绍了需要帮助入门Boost.Python的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立我的第一个Boost.Python的例子。

I'm trying to build my first Boost.Python example.

#include <iostream>
#include <boost/python.hpp>

using namespace boost::python;


class Hello {

public:
    std::string greet() {
        std::cout << "Hello World" << std::endl;
    }
};


BOOST_PYTHON_MODULE(hello)
{
    class_<Hello>("Hello")
        .def("greet", &Hello::greet);
}

int main() {
    std::cout << "Boost.Python Test" << std::endl;
    Hello hello;
    hello.greet();
    return 0;
}

编辑:Python的开发头不翼而飞,作为@cdhowie指出。我发现,其中包括必要的头文件。现在,链接器在抱怨:

Python development headers were missing, as @cdhowie has pointed out. I have found and included the required header files. Now the linker is complaining:

   10:43:58 **** Build of configuration BoostPythonTest-DPar for project BoostPythonTest 

****
make all 
Building file: ../src/BoostPythonTest.cpp
Invoking: GCC C++ Compiler
/usr/local/bin/g++-4.7 -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I/usr/include -I/usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 -O0 -g3 -p -pg -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/BoostPythonTest.d" -MT"src/BoostPythonTest.d" -o "src/BoostPythonTest.o" "../src/BoostPythonTest.cpp"
Finished building: ../src/BoostPythonTest.cpp

Building target: libBoostPythonTest-DPar.dylib
Invoking: MacOS X C++ Linker
/usr/local/bin/g++-4.7 -L/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/config-3.3m -L/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib -L/usr/local/Cellar/boost/1.51.0/lib -std=c++11 -Xlinker -ldl -framework CoreFoundation -lpython3.3m -dynamiclib -o "libBoostPythonTest-DPar.dylib"  ./src/BoostPythonTest.o   -lpython3.3m -lboost_python-mt -lpython3.3
Undefined symbols for architecture x86_64:
  "boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:
      _PyInit_hello in BoostPythonTest.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [libBoostPythonTest-DPar.dylib] Error 1

我已经联系到 -lpython3.3m -lboost_python-MT -lpython3.3 - ?还有什么是缺少

I've already linked to -lpython3.3m -lboost_python-mt -lpython3.3 - what else is missing?

编辑:我想我已经联系到 python3.3-配置列出一切。链接仍然没有由于缺少符号的工作。

I think I've linked to everything which python3.3-config lists. Linking still does not work because of missing symbols.

推荐答案

在这个特殊的链接器错误发生时,它往往是对的Python的一个版本的应用程序构建,比如Python 3.x的头文件的结果,而 boost_python 图书馆建针对不同的版本,如2.X。

When this particular linker error occurs, it is often the result of the application building against one version of Python, such as Python 3.x header files, while the boost_python library was built against a difference version, such as 2.x.

在<一个href=\"http://svn.boost.org/svn/boost/tags/release/Boost_1_53_0/boost/python/module_init.hpp\"><$c$c>boost/python/module_init.hpp,对Python的3​​.x的构建时,的init_module 函数具有以下签名:

PyObject* boost::python::detail::init_module(PyModuleDef&, void(*)());

和Python的对抗2.x的构建时,以下签名:

and the following signature when building against Python 2.x:

PyObject* boost::python::detail::init_module(char const* name, void(*)());

如可在<一待观察href=\"http://svn.boost.org/svn/boost/tags/release/Boost_1_53_0/libs/python/src/module.cpp\">implementation ,只是功能之一将是在Boost.Python库,present。因此,给定Boost.Python库,被链接的,而连接器只抱怨不能够解决3.x的的init_module 函数,那么就很有可能该Boost.Python的图书馆建对一个Python 2.x的版本,而应用code已建成对Python的3​​.x的头文件。您可以通过倾销的Boost.Python库的符号验证这一点,并检查的init_module 签名。

As can be seen in the implementation , only one of the functions will be present in the Boost.Python library. Thus, given the Boost.Python library is being linked in, and the linker is only complaining about not being able to resolve the 3.x init_module function, then it is very likely that the Boost.Python library was built against a Python 2.x version, while the application code has been built against Python 3.x header files. You can verify this by dumping the Boost.Python library's symbols and check the init_module signature.

要解决此问题,建立与相同版本的Python从Boost.Python的是构建的应用程序。在这种情况下,无论是:

To resolve this, build the application with the same version of Python from which Boost.Python was built. In this case, either:


  • 构建与Python 2.x的头文件针对的Python 2.x的库的应用和链接。

  • 构建Boost.Python的对Python的3​​.x的<一href=\"http://www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html#get-boost\">This文档描述了打造升压的步骤,文档进入详细为Boost.Python的。它可能需要明确提供了Python可执行从Boost.Python的将使用在引导工艺打造对 - 与Python的参数。

  • Build the application with Python 2.x header files and link against Python 2.x libraries.
  • Build Boost.Python against Python 3.x. This documentation describes the steps to build Boost, and this documentation goes into detail for Boost.Python. It may be necessary to explicitly provide the Python executable from which Boost.Python will build against during the bootstrap process by using the --with-python argument.

这篇关于需要帮助入门Boost.Python的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆