9

I have a whole bunch of working C++ code that I want to write Python bindings for. I'm trying to use Boost.Python since it seems to be the easiest way to get this working, but it isn't cooperating. Here's part of the code for the extension module I'm trying to build:

BOOST_PYTHON_MODULE(libpcap_ext) {
    using namespace boost::python;
    class_<PacketEngine>("PacketEngine")
        .def("getAvailableDevices", &PacketEngine_getAvailableDevices);
}

Bjam seems to be a pain and refuses to recognize my Pythonpath or allow me to link with libpcap, so I'm using CMake. Here's my CMakeLists file, which can import and build everything just fine (outputs libpcap.so as expected):

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE "DEBUG")
  #SET(CMAKE_BUILD_TYPE "RELEASE")
  #SET(CMAKE_BUILD_TYPE "RELWITHDEBINFO")
  #SET(CMAKE_BUILD_TYPE "MINSIZEREL")
ENDIF()

FIND_PACKAGE(Boost 1.55.0)
find_package(PythonLibs REQUIRED)
IF(Boost_FOUND)
  INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "${PYTHON_INCLUDE_DIRS}")
  SET(Boost_USE_STATIC_LIBS OFF)
  SET(Boost_USE_MULTITHREADED ON)
  SET(Boost_USE_STATIC_RUNTIME OFF)
  FIND_PACKAGE(Boost 1.55.0 COMPONENTS python)

  ADD_LIBRARY(pcap_ext MODULE PacketWarrior/pcap_ext.cc PacketWarrior/PacketEngine.h PacketWarrior/PacketEngine.cc PacketWarrior/Packet.h PacketWarrior/Packet.cc)
  TARGET_LINK_LIBRARIES(pcap_ext pcap)
  TARGET_LINK_LIBRARIES(pcap_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ELSEIF(NOT Boost_FOUND)
  MESSAGE(FATAL_ERROR "Unable to find correct Boost version. Did you set BOOST_ROOT?")
ENDIF()

ADD_DEFINITIONS("-Wall")

And my pcap.py file that attempts to utilize the module:

import libpcap_ext
engine = libpcap_ext.PacketEngine()
print engine.getAvailableDevices()

But whenever I try to run the module, I get the following error:

Traceback (most recent call last):
  File "../pcap.py", line 2, in <module>
    engine = libpcap_ext.PacketEngine()
TypeError: __init__() should return None, not 'NoneType

I'm assuming it's because Boost.Python is trying to use Python 3 and my system default is Python 2.7.3. I've tried changing my user-config.jam file (in my boost_1_55_0 directory) to point to Python 2.7 and tried building:

# Configure specific Python version.
# using python : 2.7 : /usr/bin/python2.7 : /usr/include/python2.7 : /usr/lib ;

Boost.Python's installation instructions [0] seem to fail for me when I try to build quickstart with bjam (lots of warnings), so I tried following the Boost Getting Started instructions [1] to build a Python header binary, which is I think what is causing this problem. Any recommendations as to how to fix this would be amazing, I've spent hours on this.

2
  • you have generated a library named libpcap.so, but inside python you are importing a library named libcap_ext. Is that right? Dec 11, 2013 at 13:12
  • no the shared library I was generating was libpcap_ext.so. This seemed to be some weird thing with CMake (which I'd still like to figure out) but I got it working with Bjam.
    – drodman
    Dec 17, 2013 at 4:48

1 Answer 1

15

This error is probably due to linking against the wrong Python library. Make sure your extension as well as the Boost Python library are linked against the Python installation you are using to import the module.

On Linux you can check against which libraries you've linked with ldd. On OS X otool -L does the same thing. So, for example

otool -L libpcap_ext.so
otool -L /path/to/libboost_python-mt.dylib

should list the Python library they are linked against.

With CMake you can use the variable PYTHON_LIBRARY to change which Python library is used. As an example, on the command line you can set it with

cmake -DPYTHON_LIBRARY="/path/to/libpython2.7.dylib" source_dir

Lastly, on OS X a quick and dirty way (i.e. without recompiling) to change the dynamically linked libraries is install_name_tool -change.

2
  • 2
    +1. Confirmed. Thank you very much! Linking to the correct python library solved it. For macports it was -DPYTHON_LIBRARY=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib
    – Stefan
    May 8, 2015 at 7:54
  • Confirmed here as well! To compile with cmake, I used homebrew. To install python with homebrew (will ends up in the folder called /usr/local/Cellar) I did "brew install python", then "brew install cmake boost-python". In this way versions are compatible, and the PYTHON_LIBRARY is "-DPYTHON_LIBRARY=/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib"
    – SeF
    Jul 22, 2016 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.