Problem building extension under Cygwin (ImportError: Bad address)

L

Lars

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
 
J

Jason Tishler

Lars,

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
 
L

Lars

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
 
J

Jason Tishler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top