C .hファイルに#definesのみが含まれている(したがって、外部にリンクするものがない)という前提で実行すると、以下はswig 2.0(http://www.swig.org/)およびpython 2.7(テスト済み)で機能します。 )。上記のように、definesだけを含むファイルの名前がjust_defines.hであるとします。
#define FOO_A 0x3
#define FOO_B 0x5
それで:
swig -python -module just just_defines.h ## generates just_defines.py and just_defines_wrap.c
gcc -c -fpic just_defines_wrap.c -I/usr/include/python2.7 -I. ## creates just_defines_wrap.o
gcc -shared just_defines_wrap.o -o _just.so ## create _just.so, goes with just_defines.py
使用法:
$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import just
>>> dir(just)
['FOO_A', 'FOO_B', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_just', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic']
>>> just.FOO_A
3
>>> just.FOO_B
5
>>>
.hファイルにエントリポイントも含まれている場合は、それらのエントリポイントを解決するために、いくつかのライブラリ(またはそれ以上)に対してリンクする必要があります。正しいライブラリを探す必要があるかもしれないので、それは解決策をもう少し複雑にします。しかし、「ケースを定義するだけ」の場合、これについて心配する必要はありません。