Making Extentions In C

Step by step guide to making a c dll with swig on Win32 with Microsoft Visual Studio

  1. Create your C file.
  2. Create an example.i file like this:
    • %module example
      extern void exampleFunction(int size);
      extern int globalVar;
      %include example.c

  3. Run Microsoft Visual Studo 6.0 (might work with others as well)
  4. Create a new project, that is a Win32 Dynamic-link Library. Name it whatever you want.
  5. Go to Build->Set Active Configuration, and set it to Release

  6. Go to Tools->Options->Directories, and add:

    • C:\Python23\Include (for Include files)
    • C:\Python23\libs (for Libary files)
  7. Click the FileView tab, and add example.c to your source files, and example.i to your “Resource files”.

  8. Right click on “example.i” under Resource files, and click settings.
  9. Click the “Custom Build” tab, and put:
    • Commands:
      C:/swigwin/swig -python $(InputPath)

  10. While still in settings, click “example” (i.e. your project name), click link options, ane name your output file:
    • _example.dll (don’t forget that underscore)
  11. Click f7 once and it should build example_wrapper.c in your project’s directory. Add that to your project files.
  12. Click f7 again

Step by step guide to making a c dll with swig on Win32 with Mingw/Msys

  1. Follow steps 1 and 2 above
  2. Create a libpython23.a and put it in your Python23/libs directory
    • Details found here

      • Basically download pexports

      • Put python23.dll in your source directory, then from msys type:
        • pexports python23.dll > python23.def

        • dlltool –dllname python23.dll –def python23.def –output-lib libpython23.a
      • Put libpython23.a goes in your Python23/libs directory
  3. Create a makefile that is something like this:
    • CC=gcc
      default: _specialsort.dll
              $(SWIG) -python specialsort.i 
              $(CC) -I$(PYTHON_INCLUDE) -L$(PYTHON_LIBRARY) --shared specialsort.c specialsort_wrap.c -lpython23 -o _specialsort.dll
              rm -f *.o
              rm -f *.dll

Step by step guide to making a c .so with swig on Linux

  1. See windows steps 1 and 2 above
  2. Create a make file that is something like this:
    • CC=gcc
              swig -python specialsort.i
              gcc -c specialsort.c specialsort_wrap.c -I$(PYTHON_INCLUDE) -I$(PYTHON_LIBRARY)
              ld -shared -o specialsort.o specialsort_wrap.o
              rm -f *.o
              rm -f *.so

How to send a python list (of doubles) to C with SWIG

    • void send_list(PyObject *pylist)
          int i, j;
          PyObject *curitem, *curval;
          max = PySequence_Fast_GET_SIZE(pylist);
          inputlist = malloc(max * sizeof *inputlist);
          newlist = malloc(max * sizeof *newlist);
          for (i = 0; i < max; i++)
              curitem = PySequence_Fast_GET_ITEM(pylist, i);
              for (j = 0; j < 3; j++)
                  curval = PySequence_Fast_GET_ITEM(curitem, j);
                  inputlist[i][j] = (float) PyFloat_AsDouble(curval);

  1. In your .i file, include something like this:
    • extern void send_list(PyObject *args);