SWIG wrapping issues with C++ --> python
Question: I'm trying to wrap a C++ file that depends on another C++ file (global.cpp) by
using SWIG. I was able to get the first one to work fine, but this nested
dependence seems to cause an issue. Here is my setup:

## position.i

    
    
    %module position
    %include global.i
    %{
      #include "pos.h"
    %}
    %include "pos.h"
    %include "global.h"
    ... (functions declared)
    

# position.cpp

    
    
    #include <algorithm> 
    #include "global.h"
    #include "pos.h"
    ...(functions implemented)
    

# position.h

prototypes

I then do this.

    
    
    swig -c++ -python -builtin position.i
    g++ -O2 -fPIC -c position.cpp
    g++ -O2 -fPIC -c -I/Users/aaron/anaconda/include/python3.5m position_wrap.cxx
    

I have the two object files and then I bind them with

    
    
    g++ -dynamiclib -lpython position.o global.o position_wrap.o -o _position.so
    

I've tried a number of different ways of doing this after perusing through SO
and I have been totally stifled.

I get an error

    
    
     ...
     "_PyUnicode_FromFormat", referenced from:
          SwigPyObject_repr(SwigPyObject*) in position_wrap.o
          SwigPyPacked_repr(SwigPyPacked*) in position_wrap.o
          SwigPyPacked_str(SwigPyPacked*) in position_wrap.o
      "_PyUnicode_FromString", referenced from:
          _PyInit__position in position_wrap.o
          SWIG_Python_DestroyModule(_object*) in position_wrap.o
          SwigPyPacked_str(SwigPyPacked*) in position_wrap.o
      "_Py_DecRef", referenced from:
          SwigPyObject_repr(SwigPyObject*) in position_wrap.o
      "__PyObject_New", referenced from:
          _PyInit__position in position_wrap.o
    ...
        ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

with compilation. I've tried different linker flags by going to the path
`python3-config --ldflags` states. I've added the `-std=libstdc++` flag. I
once somehow got the module to generate, but upon import to python was faced
with :

    
    
    ImportError:   dlopen(/Users/aaron/Desktop/swigdPython/src/_position.cpython-35m-darwin.so, 2):
        Symbol not found: __Z10e_to_ed Referenced from:
            /Users/aaron/Desktop/swigdPython/src/_position.cpython-35m-darwin.so
        Expected in: dynamic lookup"
    

I'm at a loss trying to figure out the proper way to link these files and was
hoping someone here had some insight.


Answer: You should specify the library last on the link command line:

    
    
    g++ -dynamiclib position.o global.o position_wrap.o -o _position.so -lpython
    

