Building a shared library (The GNU Modula-2 Compiler)

### 2.14 Building a shared library ¶

This section describes building a tiny shared library implemented in Modula-2 and built with `libtool`. Suppose a project consists of two definition modules and two implementation modules and a program module `a.def`, `a.mod`, `b.def`, `b.mod` and `c.mod`. The first step is to compile the modules using position independent code. This can be achieved by the following three commands:

    libtool --tag=CC --mode=compile gm2 -g -c a.mod -o a.lo
    libtool --tag=CC --mode=compile gm2 -g -c b.mod -o b.lo
    libtool --tag=CC --mode=compile gm2 -g -c c.mod -o c.lo

The second step is to generate the shared library initialization and finalization routines. We can do this by asking gm2 to generate a list of dependent modules and then use this to generate the scaffold. We also must compile the scaffold.

    gm2 -c -g -fmakelist c.mod
    gm2 -c -g -fmakeinit -fshared c.mod
    libtool --tag=CC --mode=compile g++ -g -c c_m2.cpp -o c_m2.lo

The third step is to link all these `.lo` files.

    libtool --mode=link gcc -g c_m2.lo a.lo b.lo c.lo \
            -L$(prefix)/lib64 \
            -rpath `pwd` -lgm2 -lstdc++ -lm -o libabc.la

At this point the shared library `libabc.so` will have been created inside the directory `.libs`.