How to produce swig interface files#
This section describes how Modula-2 implementation modules can be called from Python (and other scripting languages such as TCL and Perl). GNU Modula-2 can be instructed to create a swig interface when it is compiling an implementation module. Swig then uses the interface file to generate all the necessary wrapping to that the desired scripting language may access the implementation module.
Here is an example of how you might call upon the services of the
Modula-2 library module NumberIO
from Python3.
The following commands can be used to generate the Python3 module:
export src=directory to the sources
export prefix=directory to where the compiler is installed
gm2 -I${src} -c -g -fswig ${src}/../../../gm2-libs/NumberIO.mod
gm2 -I${src} -c -g -fmakelist ${src}/../../../gm2-libs/NumberIO.mod
gm2 -I${src} -c -g -fmakeinit -fshared \
${src}/../../../gm2-libs/NumberIO.mod
swig -c++ -python3 NumberIO.i
libtool --mode=compile g++ -g -c -I${src} NumberIO_m2.cpp \
-o NumberIO_m2.lo
libtool --tag=CC --mode=compile gm2 -g -c \
-I${src}../../../gm2-libs \
${src}/../../../gm2-libs/NumberIO.mod -o NumberIO.lo
libtool --tag=CC --mode=compile g++ -g -c NumberIO_wrap.cxx \
-I/usr/include/python3 -o NumberIO_wrap.lo
libtool --mode=link gcc -g NumberIO_m2.lo NumberIO_wrap.lo \
-L${prefix}/lib64 \
-rpath `pwd` -lgm2 -lstdc++ -lm -o libNumberIO.la
cp .libs/libNumberIO.so _NumberIO.so
The first four commands, generate the swig interface file
NumberIO.i
and python wrap files NumberIO_wrap.cxx
and
NumberIO.py
. The next three libtool
commnads compile
the C++ and Modula-2 source code into .lo
objects. The last
libtool
command links all the .lo
files into a
.la
file and includes all shared library dependencies.
Now it is possible to run the following Python script
(called testnum.py
):
import NumberIO
print ("1234 x 2 =", NumberIO.NumberIO_StrToInt("1234")*2)
like this:
$ python3 testnum.py
1234 x 2 = 2468
See How to produce a Python module for another example which
uses the UNQUALIFIED
keyword to reduce the module name clutter
from the viewport of Python3.