Boost.Python Hello World on Mac OS X
Asked Answered
M

1

11

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
Mosesmosey answered 17/2, 2015 at 21:40 Comment(1)
Can you try something like: BOOST_ROOT=/usr/local/Cellar/boost BOOST_BUILD_PATH=/usr/local/Cellar/boost/tools/build/src bjam? I had a similar issue once and I found that explicitly telling bjam where to look for things via environment variables solved my issue. Not sure if it helps.Cordiecordier
T
1

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!'
Twedy answered 9/8, 2017 at 15:24 Comment(3)
I don't know why but I could not get this method to work. However, I did find a way to make this simple example compile and work by installing with Homebrew the python3 support for boost python. I used the command, brew install boost-python --with-python3 Then I used cmake to recompile and it worked.Luciferous
try looking at this: github.com/flipdazed/boost-python-hello-world also note that I was using python 2.7.14Twedy
I am trying the command brew install boost --with-python --build-from-source but it;s not working for me. I am using python3 and brew version 3.1.12. can you please help me hereHectic

© 2022 - 2024 — McMap. All rights reserved.