Рабочий пример (Windows).Важным моментом является то, что вы должны создать экземпляр каждого шаблона, который хотите предоставить, с помощью оператора %template
(см. x.i
файл) и присвоить ему допустимое имя Python.1007 * xh
#pragma once
#ifdef X_EXPORTS
#define X_API __declspec(dllexport)
#else
#define X_API __declspec(dllimport)
#endif
#include <map>
typedef std::map<int,int> mapii_t;
X_API mapii_t getMap();
xi
%module x
%{
#include "x.h"
%}
// Let swig understand __declspec and other "window-isms"
%include <windows.i>
%include <std_map.i>
// instantiate template and give it a Pythonic name.
%template(mapii_t) std::map<int,int>;
%include "x.h"
makefile
_x.pyd: x_wrap.cxx x.dll
cl /LD /W3 /MD /EHsc /IC:\Python27\include x_wrap.cxx -link /OUT:_x.pyd /libpath:C:\Python27\libs x.lib
x.dll: x.cpp
cl /LD /W4 /MD /EHsc x.cpp
x_wrap.cxx: x.i
swig -c++ -python x.i
Использование:
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import x
>>> a=x.getMap()
>>> a
<x.mapii_t; proxy of <Swig Object of type 'std::map< int,int > *' at 0x022C4200> >
>>> for k,v in a.items():
... print k,v
...
1 2
4 8
5 10