Step by step guide to making a c dll with swig on Win32 with Microsoft Visual Studio
- Create your C file.
- Create an example.i file like this:
-
%module example %{ extern void exampleFunction(int size); extern int globalVar; %} %include example.c
-
- Run Microsoft Visual Studo 6.0 (might work with others as well)
- Create a new project, that is a Win32 Dynamic-link Library. Name it whatever you want.
-
Go to Build->Set Active Configuration, and set it to Release
-
Go to Tools->Options->Directories, and add:
- C:\Python23\Include (for Include files)
- C:\Python23\libs (for Libary files)
-
Click the FileView tab, and add example.c to your source files, and example.i to your “Resource files”.
- Right click on “example.i” under Resource files, and click settings.
- Click the “Custom Build” tab, and put:
-
Commands: C:/swigwin/swig -python $(InputPath) Outputs: example_wrap.c
-
- 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)
- Click f7 once and it should build example_wrapper.c in your project’s directory. Add that to your project files.
- Click f7 again
Step by step guide to making a c dll with swig on Win32 with Mingw/Msys
- Follow steps 1 and 2 above
- Create a libpython23.a and put it in your Python23/libs directory
- Create a makefile that is something like this:
-
CC=gcc SWIG=/c/swigwin/swig.exe PYTHON_INCLUDE=/c/Python23/include PYTHON_LIBRARY=/c/Python23/libs default: _specialsort.dll _specialsort.dll: $(SWIG) -python specialsort.i $(CC) -I$(PYTHON_INCLUDE) -L$(PYTHON_LIBRARY) --shared specialsort.c specialsort_wrap.c -lpython23 -o _specialsort.dll clean: rm -f *.o rm -f *.dll
-
Step by step guide to making a c .so with swig on Linux
- See windows steps 1 and 2 above
- Create a make file that is something like this:
-
CC=gcc SWIG=swig PYTHON_INCLUDE=/usr/local/include/python2.3/ PYTHON_LIBRARY=/usr/local/lib/python2.3/config/ default: _specialsort.so _specialsort.so: swig -python specialsort.i gcc -c specialsort.c specialsort_wrap.c -I$(PYTHON_INCLUDE) -I$(PYTHON_LIBRARY) ld -shared -o _specialsort.so specialsort.o specialsort_wrap.o clean: 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); } } }
-
- In your .i file, include something like this:
-
extern void send_list(PyObject *args);
-