Attached are a C file, a .i file, and a Makefile that I thought could produce a Python-loadable binary. Also, I included a text file with the output I got showing the failure.

It looks like I didn't include something vital but I don't know what. Can anyone help me out?

I don't know mac OSX, but the missing symbols are in the python library, so perhaps you should add a -lpython somewhere, probably on the line which creates the .dynlib.

I tried adding "-lpython" at the end of the part of the Makefile that makes the _palindrome.dynlib, and that let it compile properly.

Python couldn't import the .dynlib file, but then I tried renaming it and it found it.

After that though, I had the issue below.

>>> import palindrome
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "palindrome.py", line 7, in <module>
    import _palindrome
ImportError: dlopen(./_palindrome.so, 2): no suitable image found.  Did find:
	./_palindrome.so: mach-o, but wrong architecture

Many people seem to use the .dylib suffix, could it be the problem ? (I'm a mac ignorant). Also look what they say HERE about os x.

Many people seem to use the .dylib suffix, could it be the problem ? (I'm a mac ignorant). Also look what they say HERE about os x.

At this point I know that Python won't import .dylib or .dynlib file, just .so. The issue now is that it didn't like the binary produced, so the file extension isn't the issue (unless it affects gcc in some way that I'm not aware of).

I just saw (late) the second thing you said, sorry I didn't respond to it. I'll look at it soon when I get a chance.

Sorry once again for not responding to the second part of your response, I totally missed it the first time I read it! I had actually tried what was on that web page already (though that is not why I didn't respond here in my last post), as it was the first thing that popped up when I Googled "python mac swig".

I do get a different error with those instruction though. Here's the error I get when trying to compile with the new Makefile (listed below the error).

d183-35-tercero-c-1:swig_play micseydel$ make
gcc -c palindrome.c
swig -python palindrome.i
gcc -c -I/usr/include/python2.5 palindrome_wrap.c
palindrome_wrap.c: In function ‘SWIG_Python_AddErrorMsg’:
palindrome_wrap.c:853: warning: format not a string literal and no format arguments
ld -bundle -flat_namespace -undefined suppress -o _palindrome.so palindrome.o palindrome_wrap.o
ld: symbol dyld_stub_binding_helper not defined (usually in crt1.o/dylib1.o/bundle1.o) for inferred architecture x86_64
make: *** [_palindrome.so] Error 1

Aaaand, the Makefile...

(note that only the top rule is changed)

_palindrome.so: palindrome.o palindrome_wrap.o
	ld -bundle -flat_namespace -undefined suppress -o _palindrome.so palindrome.o palindrome_wrap.o

palindrome_wrap.o: palindrome_wrap.c
	gcc -c -I/usr/include/python2.5 palindrome_wrap.c

palindrome_wrap.c: palindrome.i
	swig -python palindrome.i

palindrome.o: palindrome.c
	gcc -c palindrome.c

clean:
	rm palindrome.o palindrome.py palindrome_wrap.c palindrome_wrap.o _palindrome.so

(Also, sorry about the Python syntax for the code tags. I'm new here and I don't know how to use them properly.)

This tutorial with the ld command seems a little old, and was certainly not written for x86_64. There are probably options to find for gcc and/or ld that would make your module. I'm afraid I can't help you more because I never compiled anything on mac OSX. Good luck !

edit: at the bottom of the tutorial page, there is a link to a zip file containing the example module. Since the author says that it worked on OS X in 2008, you should have a look at the makefile and write something similar ;)

This tutorial with the ld command seems a little old, and was certainly not written for x86_64. There are probably options to find for gcc and/or ld that would make your module. I'm afraid I can't help you more because I never compiled anything on mac OSX. Good luck !

edit: at the bottom of the tutorial page, there is a link to a zip file containing the example module. Since the author says that it worked on OS X in 2008, you should have a look at the makefile and write something similar ;)

I've tried using gcc instead of ld directly but that didn't work.

Also, way back when I used that site (before coming and asking in a forum), I tried their Makefile without success.

I tried it again, then tried tweaking the location of things for the finkincludes part, and still nothing works (I get the "... inferred architecture x86_64" error at best).

I tried changing "-I/sw/include/${python}" on line 30 to the Linux version, and that gets me farther in the compilation than without modification but still not all the way.


Is there more relevant information that I could provide that I'm not?

mikonapoli at http://python-forum.org/pythonforum/viewtopic.php?f=1&t=16848&p=82550 figured it out.

I've uploaded fully functional zip files (fully functional for me), but here's the C Makefile for those who want a quick look -

_palindrome.so: palindrome_wrap.o
	gcc -dynamiclib -lpython *.o -o _palindrome.so

palindrome_wrap.o: palindrome_wrap.c
	gcc -fPIC palindrome_wrap.c  -o palindrome_wrap.o -I/usr/include/python2.5/ -L/usr/lib/python2.5 -c

palindrome_wrap.c: palindrome.c palindrome.i
	swig -python -o palindrome_wrap.c palindrome.i

clean:
	rm _palindrome.so palindrome_wrap.* palindrome.py*

Sorry I couldn't not use Python syntax for the Makefile. I tried several different things but nothing meaningful changed it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.