title: Python SWIG wrapper for c++ on OSX ImportError
body: <p>I've been trying to write a python wrapper for a C++ vision library that's built on top of OpenCV, but have been running into the following error message when I try to import the compiled module in Python:</p>
  
  <pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "BGSPy.py", line 21, in &lt;module&gt;
    _BGSPy = swig_import_helper()
  File "BGSPy.py", line 20, in swig_import_helper
    return importlib.import_module('_BGSPy')
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
  __import__(name)
  ImportError: dynamic module does not define init function (init_BGSPy)
  </code></pre>
  
  <p>Here is the CMakeList.txt that I'm using to build the project:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8)
  
  project(bgs)
  
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
  #set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)
  
  # compilation mode setup
  set(CMAKE_BUILD_TYPE Release)
  #set(CMAKE_BUILD_TYPE Debug)
  
  SET(OpenCV_STATIC OFF)
  
  IF(UNIX)
    # add some standard warnings
    ADD_DEFINITIONS(-Wno-variadic-macros -Wno-long-long -Wall -Wextra -Winit-self -Woverloaded-virtual -Wsign-promo -Wno-unused-parameter -pedantic -Woverloaded-virtual -Wno-unknown-pragmas)
  
    # -ansi does not compile with sjn module
    #ADD_DEFINITIONS(-ansi)
  
    # if you like to have warinings about conversions, e.g. double-&gt;int or double-&gt;float etc., or float compare
    #ADD_DEFINITIONS(-Wconversion -Wfloat-equal)
  endif(UNIX)
  
  if(APPLE)
    cmake_policy(SET CMP0042 NEW)
  endif()
  
  find_package(OpenCV REQUIRED)
  
  message(STATUS "OpenCV library status:")
  message(STATUS "    version: ${OpenCV_VERSION}")
  message(STATUS "    libraries: ${OpenCV_LIBS}")
  message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
  
  if(${OpenCV_VERSION} VERSION_LESS 3)
    message (FATAL_ERROR "This branch of BGSLibrary is not compatible with your OpenCV version: ${OpenCV_VERSION}")
  endif()
  
  file(GLOB sources FrameProcessor.cpp PreProcessor.cpp VideoAnalysis.cpp  VideoCapture.cpp)
  file(GLOB main Main.cpp)
  file(GLOB demo Demo.cpp)
  file(GLOB demo2 Demo2.cpp)
  file(GLOB bgspy BGSPy.cpp)
  
  list(REMOVE_ITEM sources ${demo} ${demo2} ${bgspy})
  
  file(GLOB_RECURSE analysis package_analysis/*.cpp)
  file(GLOB_RECURSE bgs package_bgs/*.cpp package_bgs/*.c)
  file(GLOB_RECURSE bgs_include package_bgs/*.h)
  
  include_directories(${CMAKE_SOURCE_DIR})
  
  add_library(bgs SHARED ${sources} ${bgs} ${analysis})
  target_link_libraries(bgs ${OpenCV_LIBS})
  set_property(TARGET bgs PROPERTY PUBLIC_HEADER ${bgs_include})
  
  add_executable(bgs_bin ${main})
  target_link_libraries(bgs_bin ${OpenCV_LIBS} bgs)
  set_target_properties(bgs_bin
    PROPERTIES OUTPUT_NAME bgs)
  
  add_executable(bgs_demo ${demo})
  target_link_libraries(bgs_demo ${OpenCV_LIBS} bgs)
  
  add_executable(bgs_demo2 ${demo2})
  target_link_libraries(bgs_demo2 ${OpenCV_LIBS} bgs)
  
  set(WRAPPER_PREFIX "BGSPy")
  
  # Swig Module
  
  FIND_PACKAGE(SWIG REQUIRED)
  INCLUDE(${SWIG_USE_FILE})
  
  FIND_PACKAGE(PythonLibs)
  INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
  
  INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
  
  SET(CMAKE_SWIG_FLAGS "")
  
  SET_SOURCE_FILES_PROPERTIES(${WRAPPER_PREFIX}.i PROPERTIES CPLUSPLUS ON)
  SET_SOURCE_FILES_PROPERTIES(${WRAPPER_PREFIX}.i PROPERTIES SWIG_FLAGS "-includeall")
  SWIG_ADD_MODULE(${WRAPPER_PREFIX} python ${WRAPPER_PREFIX}.i ${WRAPPER_PREFIX}.cpp)
  SWIG_LINK_LIBRARIES(${WRAPPER_PREFIX} ${PYTHON_LIBRARIES} ${OpenCV_LIBS} bgs)
  
  INSTALL(TARGETS bgs
      bgs_bin
    RUNTIME DESTINATION bin COMPONENT app
    LIBRARY DESTINATION lib COMPONENT runtime
    ARCHIVE DESTINATION lib COMPONENT runtime
    PUBLIC_HEADER DESTINATION include/package_bgs COMPONENT dev
    FRAMEWORK DESTINATION "/Library/Frameworks"
  )
  </code></pre>
  
  <p>The project builds successfully, but I cannot import the module it generates. Any help would be greatly appreciated. </p>
  
  <p>EDIT: Here is the content of my BGSPy.i file</p>
  
  <pre><code>%module BGSPy
  %include &lt;std_string.i&gt;
  %{
  #include "BGSPy.h"
  %}
  
  extern int adaptiveSOM(const std::string fName);
  </code></pre>