3

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.

3 Answers 3

2

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

g++ -dynamiclib position.o global.o position_wrap.o -o _position.so -lpython
Sign up to request clarification or add additional context in comments.

5 Comments

The result of this is a much smaller error message, but still receiving an undefined symbols error.
@Anisotropic: You may need to link with other libraries. Which symbols are still undefined?
Compiler barfs out undefined "_PyInstanceMethod_New" , "_PyModule_Create2", "_PyUnicode_Concat", "_PyUnicode_FromFormat", "_PyUnicode_FromString" reference from the wrapper object. the invocation is Thread model: posix "/Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/ld" -demangle -dynamic -dylib -arch x86_64 -macosx_version_min 10.10.0 -o _sunposition.so sunpos.o global.o sunpos_wrap.o -lpython -lc++ -lSystem /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Do you see the libpython.dylib in the /usr/lib directory?
Yes along with the 2.7 version of it
2

The ordering might matter here, i.e.

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

instead of

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

Comments

1

I got this to finally work using this command:

g++ -dynamiclib -o _position.so position_wrap.o position.o global.o -
I/Users/aaron/anaconda/include/python3.5m/ - L/Users/aaron/anaconda/lib/ -I/Users/aaron/Desktop/swigdPython/src -lpython3.5

I then couldn't get the module to load until exporting an environment variable:

export DYLD_LIBRARY_PATH=/Users/aaron/anaconda/lib:$DYLD_LIBRARY_PATH

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.