From: Haoyu B. <div...@gm...> - 2009-03-26 17:28:32
|
On Fri, Mar 27, 2009 at 12:26 AM, <mi...@nc...> wrote: > Hi all: > > I'm just getting started with SWIG and I'm trying to compile to a dll using the small example file in the docs. I want to build using NMake. I can get the makefile to build a dll, but when I try to access the functions within the dll from Python, it keeps telling me the function does not exist. I know I'm doing something simple wrong, but can't figure out what. I would appreciate your help -- > > Here are my files: > > /* File : example.c */ > > #include <time.h> > double My_variable = 3.0; > > int fact(int n) { > if (n <= 1) return 1; > else return n*fact(n-1); > } > > int my_mod(int x, int y) { > return (x%y); > } > > char *get_time() > { > time_t ltime; > time(<ime); > return ctime(<ime); > } > > /* File: example.i */ > %module example > > %{ > /* Put header files here or function declarations like below */ > extern double My_variable; > extern int fact(int n); > extern int my_mod(int x, int y); > extern char *get_time(); > %} > > extern double My_variable; > extern int fact(int n); > extern int my_mod(int x, int y); > extern char *get_time(); > > /* File: Makefile */ > # Makefile for building a Python extension > > SRCS = example.c > IFILE = example > INTERFACE = $(IFILE).i > WRAPFILE = $(IFILE)_wrap.c > > # Location of the Visual C++ tools (32 bit assumed) > > TOOLS = c:\Program Files\microsoft visual studio\vc98 > TARGET = example.dll > CC = "$(TOOLS)\bin\cl.exe" > LINK = "$(TOOLS)\bin\link.exe" > INCLUDE32 = -I"$(TOOLS)\include" > MACHINE = IX86 > > # C Library needed to build a DLL > > DLLIBC = msvcrt.lib oldnames.lib > > # Windows libraries that are apparently needed > WINLIB = kernel32.lib advapi32.lib user32.lib gdi32.lib comdlg32.lib winspool.lib > > # Libraries common to all DLLs > LIBS = $(DLLIBC) $(WINLIB) > > # Linker options > LOPT = -debug:full -debugtype:cv /NODEFAULTLIB /RELEASE /NOLOGO /MACHINE:$(MACHINE) -entry:_DllMainCRTStartup@12 -dll > > # C compiler flags > > CFLAGS = /Z7 /Od /c /nologo > PY_INCLUDE = -Ic:\python25\include -Ic:\python25 > PY_LIB = c:\python25\libs\python25.lib > PY_FLAGS = /D__WIN32__ > > python:: > swig -python -o $(WRAPFILE) $(INTERFACE) > $(CC) $(CFLAGS) $(PY_FLAGS) $(PY_INCLUDE) $(INCLUDE32) $(SRCS) $(WRAPFILE) > set LIB=$(TOOLS)\lib > $(LINK) $(LOPT) -out:_example.dll $(LIBS) $(PY_LIB) example.obj example_wrap.obj > > /* Invoking via Python from command line */ > C:\Program Files\swig_example>python > Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> from ctypes import * >>>> elib = windll.LoadLibrary("example.dll") >>>> elib.fact(5) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "c:\python25\lib\ctypes\__init__.py", line 353, in __getattr__ > func = self.__getitem__(name) > File "c:\python25\lib\ctypes\__init__.py", line 358, in __getitem__ > func = self._FuncPtr((name_or_ordinal, self)) > AttributeError: function 'fact' not found >>>> > > I'm a bit confused. SWIG is not relay on ctypes. This should work: import example example.fact(5) -- Haoyu Bai |