469,648 Members | 1,168 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Problem building extension under Cygwin (ImportError: Bad address)

Hi,

I have problems building a simple handcoded C-extension (hello.c) with
Cygwin and gcc3.4.4. I hope somebody has encountered the same problem
before, and has some advice.

The extension works just fine with Linux:

gcc -c hello.c -I/usr/local/include/python2.4/
ld -shared hello.o -o hello.so -L /usr/local/lib/ -lpython2.4 -lc
python -c'import hello;q = hello.message("Lars");print q'
Hello, Lars

But doing a similar compile under Cygwin doesn't work so well:
(same options, but the output is called hello.dll and the directories
are a bit different)

$ python -c"import hello"
Traceback (most recent call last):
File "<string>", line 1, in ?
ImportError: Bad address

btw:
hello.c is from "Programming Python 2nd ed." by Mark Lutz. It's
published by O'Reilly. The code can be downloaded from:
http://examples.oreilly.com/python2/Examples.zip .
hello.c is in the directory PP2E\Integrate\Extend\Hello.
-Lars

May 4 '06 #1
4 1922
Lars,

On Thu, May 04, 2006 at 03:50:13AM -0700, Lars wrote:
I have problems building a simple handcoded C-extension (hello.c) with
Cygwin and gcc3.4.4. I hope somebody has encountered the same problem
before, and has some advice.

[snip]


The following works for me:

$ gcc -shared -I/usr/include/python2.4 -o hello.dll hello.c -L/usr/lib/python2.4/config -lpython2.4
$ python -c'import hello;q = hello.message("Lars");print q'
Hello, Lars
$ python hellouse.py
Hello, C
Hello, module /tmp/Examples/PP2E/Integrate/Extend/Hello/hello.dll
Hello, 0
Hello, 1
Hello, 2

Note I had to apply the attached patch to get hello.c to compile.

Jason

--
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D 8784 1AFD E4CC ECF4 8EF6

May 4 '06 #2
Thanks Jason, it works now.

There seems to be some sort of issue with ld in cygwin when compiling
shared libraries (e.g. DLL's).
This worked on my Cygwin installation also:

------------------------------
gcc hello.c -I/usr/include/python2.4/ -L/usr/lib/python2.4/config/
-lpython2.4 -shared -o hello.dll
python hellouse.py
(gives the right answer)
--------------------------------

But first compiling hello.c with gcc, then linking it with gnu-ld just
won't work. I only really need to compile one C-file to a shared
library so it doesn't matter so much for me. But bigger projects will
have a problem..

------------------------------------------
gcc -c hello.c -I/usr/include/python2.4/
ld -shared hello.o -o hello.so -L /usr/lib/python2.4/config -lpython2.4
-lc
python hellouse.py
Traceback (most recent call last):
File "hellouse.py", line 1, in ?
import hello
ImportError: Bad address
-------------------------------------------

-Lars

May 4 '06 #3
On Thu, May 04, 2006 at 06:23:23AM -0700, Lars wrote:
But first compiling hello.c with gcc, then linking it with gnu-ld just
won't work. I only really need to compile one C-file to a shared
library so it doesn't matter so much for me. But bigger projects will
have a problem..


No, it works if you drive it from gcc:

$ gcc -I/usr/include/python2.4 -c hello.c
$ gcc -shared -o hello.dll hello.o -L/usr/lib/python2.4/config -lpython2.4
$ python hellouse.py
Hello, C
Hello, module /tmp/examples/PP2E/Integrate/Extend/Hello/hello.dll
Hello, 0
Hello, 1
Hello, 2

BTW, if you use Distutils, then it will just work...

Jason

--
PGP/GPG Key: http://www.tishler.net/jason/pubkey.asc or key servers
Fingerprint: 7A73 1405 7F2B E669 C19D 8784 1AFD E4CC ECF4 8EF6
May 4 '06 #4
Great!
Thanks for the advice.

Lars

May 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil Schmidt | last post: by
reply views Thread by Jose Vicente Nunez Z | last post: by
7 posts views Thread by Carl Waldbieser | last post: by
3 posts views Thread by Steve Holden | last post: by
2 posts views Thread by maxwell | last post: by
reply views Thread by Stewart Midwinter | last post: by
1 post views Thread by lennyw | last post: by
reply views Thread by Darrin Thompson | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.