Compiling C++ with SWIG on Mac OS X

Nick S. picture Nick S. · Feb 9, 2013 · Viewed 7.2k times · Source

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.

Answer

Nick S. picture Nick S. · Feb 9, 2013

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