22

I am trying to compile a C++ extension using Swig for Mac OS X. I have run into a few linker errors though. The basic tutorial for Python Swig also seems to fail on Mac:

http://www.swig.org/Doc1.3/Python.html#Python_nn10

swig -c++ -python example.i
g++ -O2 -fPIC -c example.cxx
g++ -O2 -fPIC -c example_wrap.cxx -I/usr/include/python2.6
g++ -shared example.o example_wrap.o -o _example.so

The first three lines work fine. The last line fails with a linker error. I tried the last line on OS X with this, and got the same error:

g++ -dynamiclib example.o example_wrap.o -o _example.so

The error from the last line is:

Undefined symbols for architecture x86_64:
  "_PyArg_ParseTuple", referenced from:
      __wrap_fact in example_wrap.o
  "_PyArg_UnpackTuple", referenced from:
      _SwigPyObject_own in example_wrap.o
  "_PyBool_FromLong", referenced from:
      _SwigPyObject_richcompare in example_wrap.o
      _SwigPyObject_own in example_wrap.o
...
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Thanks.

Nick S.
  • 1,643
  • 1
  • 12
  • 13
  • 5
    `-lpython` at the end of the compile line should address the linking issue, but Mac shared objects have the extension `.dylib` – Anya Shenanigans Feb 08 '13 at 23:27
  • Thanks, @Petesh -- that works! I was getting hung up on a different issue with my main Swig code (undefined symbols for some functions, which I commented out in the swig.i file). Basic Swig functionality works now. – Nick S. Feb 09 '13 at 03:11
  • 6
    Aaaaand again the close-useful-thread fairys strike. This is a common and broad problem working with multiple SWIG based libraries when on a mac. Why the hell would you close it?! – Shayne Sep 20 '17 at 07:09

1 Answers1

18

For completeness -- thanks Petesh.

swig -c++ -python example.i
g++ -O2 -fPIC -c example.cxx
g++ -O2 -fPIC -c example_wrap.cxx -I/usr/include/python2.6
g++ -lpython -dynamiclib example.o example_wrap.o -o _example.so
Jans
  • 11,064
  • 3
  • 37
  • 45
Nick S.
  • 1,643
  • 1
  • 12
  • 13