graphics.hatenablog.com

技術系テクニカルアーティストのあれこれ

SWIGメモ。

// test.h
#pragma once
class TestClass
{
public:
	TestClass();
	~TestClass();

	void f();
};
// test.cpp
#include "test.h"
#include <iostream>

TestClass::TestClass()
{
	std::cout << "test()" << std::endl;
	std::flush(std::cout);
}


TestClass::~TestClass()
{
	std::cout << "~test()" << std::endl;
	std::flush(std::cout);
}

void TestClass::f()
{
	std::cout << "f()" << std::endl;
	std::flush(std::cout);
}
%module test
%{
#include "test.h"
%}
%include "test.h"
>swig.exe -c++ -python test.i
>>> import test
>>> for i in xrange(3):
...     t = test.TestClass()
...     t.f()
...
test()
f()
test()
~test()
f()
test()
~test()
f()
>>> quit()
~test()

test.pyd じゃなくて _test.pyd でビルドしないと ImportError になるの、すごく罠っぽい。
なんか間違ってるのかなぁ。。