1

我正在尝试包装一些 C++ 函数以在 python 中使用。例如,这里是 boost Python 教程中的函数。

//  Copyright Joel de Guzman 2002-2004. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
//  or copy at http://www.boost.org/LICENSE_1_0.txt)
//  Hello World Example from the tutorial
//  [Joel de Guzman 10/9/2002]

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

当我将其编译成 .pyc 文件并尝试将其导入 python 时,我收到错误:

ImportError:C:\hello_ext.pyc 中的错误幻数

我已经使用另一个论坛的方法检查了幻数,它似乎确实是错误的。我四处搜索,但找不到有关此错误消息的任何有用信息。我怀疑这是我的 Visual Studio 项目文件中的错误设置,或者可能与我编译 boost 的方式有关?

我正在使用 Visual Studio 2010 服务包 1、python 2.7.3 和 boost 1.53

我使用以下选项编译了 boost。

b2 install toolset=msvc-10.0 variant=debug,release threading=multi link=shared runtime-link=shared --prefix="C:\boost"
4

1 回答 1

1

当你编译 boost python 时,你应该得到一个共享库(例如我的机器上的 *.so),而不是 .pyc 文件。

这是一个关于如何构建 boost python 扩展的页面: http ://wiki.python.org/moin/boost.python/BuildingExtensions

于 2013-03-05T19:27:02.903 回答