Find the answer to your Linux question:
Results 1 to 2 of 2
Has anyone one successfully compiled a C shared object for Python 2.6 using Swig? I've been trying but I keep getting this error when I try to import - "dynamic ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    Python and Swig

    Has anyone one successfully compiled a C shared object for Python 2.6 using Swig?
    I've been trying but I keep getting this error when I try to import - "dynamic module does not define init function (initarith)"

    arith.c
    Code:
    int add(int a, int b)
    {
          return a + b;
    }
    Compile lines:

    swig -python -module arith arith.c

    gcc -I/usr/include/python2.6 -fPIC -c arith.c arith_wrap.c

    ld -shared -o arith.so arith.o arith_wrap.o

    I tried several sites and tutorials but I always get the same error. So has anyone got this to work? Does anyone know what's wrong?...Thanks...Gerard4143

    Does anyone know how to bring C code into Python? Any answer will be appreciated..
    Make mine Arch Linux

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Got it working...Gerard4143

    MyLibrary2.c
    Code:
    unsigned short getidt()
    {
    	char ch1[(sizeof (unsigned short) + sizeof(void*))];
    
    	__asm__ __volatile__
    	(
    	 	"sidt	%0\n\t"
    		:"=m"(ch1)
    	);
    	return *(unsigned short*)ch1;
    }
    MyLibrary2.h
    Code:
    unsigned short getidt();
    MyLibrary2.i
    Code:
    %module MyLibrary2
    
    %{
    #include "MyLibrary2.h"
    %}
    
    unsigned short getidt();
    And finally the compile lines:

    Code:
    swig -python MyLibrary2.i
    
    gcc -fPIC -c MyLibrary2.c
    
    gcc -fPIC -c -I/usr/include/python2.6 MyLibrary2_wrap.c
    
    gcc -shared MyLibrary.o MyLibrary2_wrap.o _MyLibrary2.so
    Start Python interrupter
    import MyLibrary2
    MyLibrary2.getidt()

    And after those simple steps...it works.....Its too hot to be doing this...Gerard4143
    Make mine Arch Linux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...