Problem building extension under Cygwin (ImportError: Bad address)

Discussion in 'Python' started by Lars, May 4, 2006.

  1. Lars

    Lars Guest

    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
     
    Lars, May 4, 2006
    #1
    1. Advertisements

  2. Lars,

    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
     
    Jason Tishler, May 4, 2006
    #2
    1. Advertisements

  3. Lars

    Lars Guest

    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, 2006
    #3
  4. 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
     
    Jason Tishler, May 4, 2006
    #4
  5. Lars

    Lars Guest

    Great!
    Thanks for the advice.

    Lars
     
    Lars, May 4, 2006
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.