[ C++で開発 ]

SWIGのインストール

SWIG:Simplified Wrapper and Interface Generator をインストールして利用するまでの記録です。

準備編

SWIGの入手

SWIG総本山のサイトからソースを入手します。

2004年9月12日現在、SWIG 1.3.22が最新です。

インストール編

Solaris 9 x86 with Sun Studio 9 Compiler

使用環境

ツール種類 パス 備考
Cコンパイラ /opt/SUNWspro/bin/cc
C++コンパイラ /opt/SUNWspro/bin/CC
makeツール /usr/ccs/bin/make

SWIGソースアーカイブの展開

まず、SWIGソースコードを作業ディレクトリに展開します。

torutk$ mkdir work
torutk$ cd work
work$ tar xzf ~/swig-1.3.22.tar.gz
work$ cd SWIG-1.3.22
SWIG-1.3.22$ 

ビルド準備(configure)

コンパイラには、Sun Studio 9のCコンパイラおよびC++コンパイラを使用します。そこで、configureのオプションで指定します。

SWIG-1.3.22$ ./configure CC=cc CXX=CC
   :
SWIG-1.3.22$

ビルド(make)

GNU makeではエラーになるようです。Solaris標準make(/usr/ccs/bin/make)を使用します。

SWIG-1.3.22$ make
   :
SWIG-1.3.22$

インストール

デフォルトでは/usr/localの下にインストールされます。

SWIG-1.3.22$ su
# PATH=$PATH:/usr/ccs/bin:/opt/SUNWspro/bin
# make install
Installing SWIG executable
Installing /usr/local/bin/swig
Installing the SWIG library
Installing /usr/local/share/swig/1.3.22/_std_deque.i
   :
Installing language specific files for tcl
Installing /usr/local/share/swig/1.3.22/tcl/cstring.i
   :
Installing language specific files for perl5
Installing /usr/local/share/swig/1.3.22/perl5/perlmain.i
   :
Installing language specific files for python
Installing /usr/local/share/swig/1.3.22/python/argcargv.i
   :
Installing language specific files for guile
Installing /usr/local/share/swig/1.3.22/guile/cplusplus.i
   :
Installing language specific files for java
Installing /usr/local/share/swig/1.3.22/java/arrays_java.i
   :
Installing language specific files for mzscheme
Installing /usr/local/share/swig/1.3.22/mzscheme/std_common.i
   :
Installing language specific files for ruby
Installing /usr/local/share/swig/1.3.22/ruby/embed.i
   :
Installing language specific files for php4
Installing /usr/local/share/swig/1.3.22/php4/std_common.i
   :
Installing language specific files for ocaml
Installing /usr/local/share/swig/1.3.22/ocaml/carray.i
   :
Installing language specific files for pike
Installing /usr/local/share/swig/1.3.22/pike/std_string.i
   :
Installing language specific files for chicken
Installing /usr/local/share/swig/1.3.22/chicken/typemaps.i
   :
Installing language specific files for csharp
Installing /usr/local/share/swig/1.3.22/std_string.i
   :
Installing language specific files for modula3
Installing /usr/local/share/swig/1.3.22/modula3/typemaps.i
   :
Installing language specific files for allegrocl
Installing /usr/local/share/swig/1.3.22/allegrocl/typemaps.i
   :
Installation complete
#

設定

GCC

取っ掛かり編

最初の例題

C++のモジュールを作成

Person.hpp
#include <string>
using std::string;

int getMaxPersons();

class Person
{
public:
  Person();
  Person(const string& aName);
  ~Person() {}
  const string& getName() const;
  void setName(const string& newName);
};

Person.cpp は略

SWIG用インタフェースファイルの定義

address.i
%module address
%{
#include "Person.hpp"
%}

%include "Person.hpp"

swigツールでC++ラッパー用コードを生成

SWIGのコマンドswigを実行して、Pythonから呼び出せるようにモジュールを作成します。

src$ swig -c++ -python -o address_wrap.cpp address.i
src$ ls
Person.cpp   address.i    address.pyc
Person.hpp   address.py   address_wrap.cpp
src$ 

C++で処理を実行する共有ライブラリファイルを生成

共有ライブラリファイルの名前は、モジュール名と共有ライブラリファイル名が合っている必要があります。通常のようにlibXXX ではなく、libは付けません。

src$ CC -c address_wrap.cpp Person.cpp -KPIC -I/opt/sfw/include/python2.2
address_wrap.cpp:
Person.cpp:
src$ CC -G -o address.so Person.o address_wrap.o
src$ ls
Person.cpp   Person.hpp   address.py   address_wrap.cpp   address.so
Person.o     address.i    address.pyc  address_wrap.o
src$

Pythonで使用する

torutk$ python
>>> import sys
>>> sys.path.append("/export/home/torutk/python")
>>> import address
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Import Error: ld.so.1: python: 重大なエラー: 再配置エラー: ファイル./address.so:
 シンボル __1cDstdMbasic_string4Ccn0ALchar_traits4Cc__n0AJallocator4Cc___2t6Mp
kcrkn0C__v_: 参照シンボルが見つかりません。
>>>