Mac OS X上的Boost.Python Hello World [英] Boost.Python Hello World on Mac OS X

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

问题描述

我正在尝试设置和编译Boost.Python的Hello World示例:解决方案

摘要

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

更长的答案

我的设置与您完全相同.我花了 ages 来使它正常工作,因为文档确实是可疑的(如您所知),并且在您知道它之前,您已经陷入了一些奇怪的兔子漏洞,试图破解make文件和BJAM安装.

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

安装

您可以通过以下命令通过homebrew获得正确的boost-python:

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

我认为brew install boost应该可以工作,但是安装量很大,两次寿命很短

提升代码

假定

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!'

I am trying to setup and compile the Hello World example for Boost.Python: http://www.boost.org/doc/libs/1_57_0/libs/python/doc/tutorial/doc/html/python/hello.html

I installed bjam, boost, boost-build, and boost-python from Homebrew:

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

My python install is also via Homebrew. I am not sure how to properly modify the example Jamroot file so that it is compatible with my system setup. I changed the boost path to : /usr/local/Cellar/boost; but I'm not sure of the other paths that need to be changed. The current setup gives me the following error:

> 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

解决方案

Summary

  1. Don't use BJAM it's a waste of your time - I am assuming your interest in BJAM is a side-product of getting your code to actually work
  2. Here is the quick-link to my github page where I do a hello_world example using namespace boost::python
  3. See my github for linking multiple boost files into one import library

Longer Answer

I have exactly the same setup as you. I spent ages getting this to work as the documentation is really shady (as you know) and before you know it you go down some weird rabbit hole trying to hack make files and BJAM installations.

You can use a setup.py as you would normally with C code as follows...

Installation

You can obtain the correct boost-python through homebrew via the command:

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

I think brew install boost should work but it's a big install and life is short to do it twice

Boost code

Assume the following code in 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

Then you can write setup.py file as

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])

Compiling

The following example can be used by:

python setup.py build_ext --inplace

which will create the following build/ directory and file:

build/
hello_ext.so

Running

This can be directly now called by python with:

In [1]: import hello_ext

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

这篇关于Mac OS X上的Boost.Python Hello World的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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