【问题标题】:Boost.Python Hello World on Mac OS XMac OS X 上的 Boost.Python Hello World
【发布时间】:2015-04-18 17:53:44
【问题描述】:

我正在尝试为 Boost.Python 设置和编译 Hello World 示例:http://www.boost.org/doc/libs/1_57_0/libs/python/doc/tutorial/doc/html/python/hello.html

我从 Homebrew 安装了 bjam、boost、boost-build 和 boost-python:

brew install bjam
brew install boost
brew install boost-build
brew install boost-python

我的 python 安装也是通过 Homebrew 安装的。我不确定如何正确修改示例 Jamroot 文件,以使其与我的系统设置兼容。我将提升路径更改为: /usr/local/Cellar/boost;,但我不确定需要更改的其他路径。当前设置给了我以下错误:

> bjam
notice: no Python configured in user-config.jam
notice: will use default configuration
Jamroot:26: in modules.load
*** argument error
* rule use-project ( id : where )
* called with: ( boost : /usr/local/Cellar/boost; project : requirements <library>/boost/python//boost_python <implicit-dependency>/boost//headers : usage-requirements <implicit-dependency>/boost//headers )
* extra argument project
/usr/local/share/boost-build/build/project.jam:1138:see definition of rule 'use-project' being called
/usr/local/share/boost-build/build/project.jam:311: in load-jamfile
/usr/local/share/boost-build/build/project.jam:64: in load
/usr/local/share/boost-build/build/project.jam:145: in project.find
/usr/local/share/boost-build/build-system.jam:535: in load
/usr/local/share/boost-build/kernel/modules.jam:289: in import
/usr/local/share/boost-build/kernel/bootstrap.jam:139: in boost-build
/usr/local/share/boost-build/boost-build.jam:8: in module scope

【问题讨论】:

  • 你能试试像:BOOST_ROOT=/usr/local/Cellar/boost BOOST_BUILD_PATH=/usr/local/Cellar/boost/tools/build/src bjam吗?我曾经遇到过类似的问题,我发现通过环境变量明确告诉 bjam 在哪里寻找东西解决了我的问题。不确定是否有帮助。

标签: python boost boost-python bjam


【解决方案1】:

总结

  1. 不要使用 BJAM,这会浪费您的时间 - 我假设您对 BJAM 的兴趣是让您的代码实际工作的副产品
  2. Here 是指向我的 github 页面的快速链接,我在其中执行 hello_world 示例 using namespace boost::python
  3. 请参阅我的github,将多个 boost 文件链接到一个导入库中

更长的答案

我的设置和你一模一样。我花了 ages 来让它工作,因为文档真的很阴暗(如你所知),在你知道它之前,你陷入了一些奇怪的兔子洞,试图破解 make 文件和 BJAM 安装。 p>

您可以像通常使用C 代码一样使用setup.py,如下所示...

安装

可以通过homebrew通过命令获取正确的boost-python:

brew install boost --with-python --build-from-source

我认为 brew install boost 应该可以工作,但它是一个大型安装,并且执行两次的寿命很短

提升代码

假设hello_ext.cpp中的代码如下

#include <boost/python.hpp>

char const* greet()
{
   return "Greetings!";
}

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

Python 设置

那么你就可以把setup.py文件写成

from distutils.core import setup
from distutils.extension import Extension

hello_ext = Extension(
    'hello_ext',
    sources=['hello_ext.cpp'],
    libraries=['boost_python-mt'],
)

setup(
    name='hello-world',
    version='0.1',
    ext_modules=[hello_ext])

编译

以下示例可供:

python setup.py build_ext --inplace

这将创建以下 build/ 目录和文件:

build/
hello_ext.so

跑步

这可以直接被python调用:

In [1]: import hello_ext

In [2]: hello_ext.greet()
Out[2]: 'Greetings!'

【讨论】:

  • 我不知道为什么,但我无法让这种方法起作用。但是,我确实找到了一种方法来使这个简单的示例编译和工作,方法是使用 Homebrew 安装对 boost python 的 python3 支持。我用了命令, brew install boost-python --with-python3 然后我用cmake重新编译,它工作了。
  • 试试看这个:github.com/flipdazed/boost-python-hello-world 另请注意,我使用的是 python 2.7.14
  • 我正在尝试命令 brew install boost --with-python --build-from-source 但它对我不起作用。我正在使用 python3 和 brew 版本 3.1.12。你能帮帮我吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2012-09-21
相关资源
最近更新 更多