Making python module with C++ using boost.python (mac)

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: 
./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.

4. Build the program to share library

Create a make script as follows:




g++ -c hello.cpp -I $PYTHON_ROOT -I $BOOST_ROOT
g++ -shared -o hello.so hello.o -lboost_python -lpython \

and run it.

  • 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.python

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


Make your Mysql friendly to Emoji character

In my last projects, Mysql is usually UTF-8 as the core encoding. However, it is not good for my current project now because we will play with Emoji Character;

Emoji Character is using an extended utf-8 Encoding, called UTF-8 mb4 (utf8mb4).

If inserting string with Emoji Character, the mysql will alert "incorrect string" and make the SQL fail.
To fix this, we need to:

  • Change the database and table to utf8mb4 encoding 
  • Change the connection encoding to utf8mb4 as well

External Reference:
  • http://mathiasbynens.be/notes/mysql-utf8mb4
  • http://info.michael-simons.eu/2013/01/21/java-mysql-and-multi-byte-utf-8-support/
  • http://my.oschina.net/wingyiu/blog/153357