How to use Boost.Python
Question: I just recently discovered Boost.Python and I am trying to figure out how it
works. I tried to go through [the tutorial on the official
website](http://www.boost.org/doc/libs/1_60_0/libs/python/doc/html/tutorial/tutorial/hello.html).
However, I got

    
    
    link.jam: No such file or directory
    

when running bjam as in the example (which appears to be just a warning), and

    
    
    Traceback (most recent call last):
    File "hello.py", line 7, in <module>
    import hello_ext
    ImportError: libboost_python.so.1.55.0: cannot open shared object file: No such file or directory
    

when running python hello.py.

I also tried to compile a module as described in [another
tutorial](http://www.shocksolution.com/python-basics-tutorials-and-
examples/linking-python-and-c-with-boostpython/) with similar results. I am
running Ubuntu14.04 with boost1.55 compiled myself.

* * *

I tried to compile the following:

    
    
    #include <boost/python.hpp>
    char const* greet()
    {
        return "hello, world";
    }
    
    BOOST_PYTHON_MODULE(hello_ext)
    {
        using namespace boost::python;
        def("greet", greet);
    }
    

with the following command from command line:

    
    
    g++ -o hello_ext.so hello.cpp -I /usr/include/python2.7/ -I /home/berardo/boost_1_55_0/ -L /usr/lib/python2.7/ -L /home/berardo/boost/lib/ -lboost_python -lpython2.7 -Wl, -fPIC -expose-dynamic 
    

which still gives me a:

    
    
    /usr/bin/ld: impossibile trovare : File o directory non esistente
    collect2: error: ld returned 1 exit status.
    


Answer: Finally, I was able to make it work. First, I fixed the linker issues, as
suggested by Dan. It finally compiled but I still got:

    
    
    ImportError: libboost_python.so.1.55.0: cannot open shared object file: No such file or directory
    

The problem was that the python module was not able to load correctly so I
needed to add another linker option. Here, I report the final Makefile:

    
    
    # location of the Python header file
    PYTHON_VERSION = 2.7
    PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
    
    # location of the Boost Python include files and library
    
    BOOST_INC = ${HOME}/boost/include
    BOOST_LIB = ${HOME}/boost/lib
    
    # compile mesh classes
    TARGET = hello_ext
    
    $(TARGET).so: $(TARGET).o
        g++ -shared -Wl,-rpath,$(BOOST_LIB) -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so
    
    $(TARGET).o: $(TARGET).C
        g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).C
    

Notice the **-Wl,-rpath,** option, which apparently makes the new created
shared library available to the python script.  
@Dan: Thanks for the valuable hints.

