1. Install Python
For most of the case, the python is already installed.The paths of my mac are as followings:
- bin: /usr/bin/python2.7
- include: /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
- library: /usr/lib/python2.7
- archive library: /usr/lib/python2.7/lib/libpython.a
2. Install boost
Download boost at http://www.boost.org/users/download/Build the library:
./bootstrap.sh
and
./b2 install
When success, can find the include and library in the following path:
include path: /usr/local/boost/boost/
library path: /usr/local/boost/stage/lib
3. Create the hello world program
Find out the detail in boost.python doc; it is clear.
http://www.boost.org/doc/libs/1_55_0/libs/python/doc/tutorial/doc/html/index.html
4. Build the program to share library
Create a make script as follows:
--------------------------------------------------------------------------------------------------------------
#!/bin/sh
BOOST_ROOT=/usr/local/boost
PYTHON_ROOT=/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
LD_LIBRARY_PATH=/usr/local/boost/stage/lib
PYTHON_LIBRARY_PATH=/usr/lib/python2.7/lib
echo $PYTHON_ROOT
g++ -c hello.cpp -I $PYTHON_ROOT -I $BOOST_ROOT
g++ -shared -o hello.so hello.o -lboost_python -lpython \
-L $LD_LIBRARY_PATH \
-L $PYTHON_LIBRARY_PATH
--------------------------------------------------------------------------------------------------------------
and run it.
Note:
- to compile successfully, need to define the correct python and boost include path
- to link successfully, need to define the boost.python and python library location
5. Play the module in python
First, need configure the environment so that python know boost.pythonDYLD_LIBRARY_PATH=$BOOST_ROOT/stage/lib
Then run ‘python’ and type
import hello
print hello_ext.greet()
If the DYLD_LIBRARY_PATH not define, may receive the following error message:
Library not loaded: libboost_python.dylib