I think I found a workaround. Go to the directory where the sf.def file was created, and type this:
dlltool --def sf.def --output-lib libsf.a
The libsf.a file will be created, and g++ should create the module correctly on the next run.
It seems that either there is a mismatch in Python and MinGW versions, or this is a bug in Python 2.7.