Previous: Compiling module sources, Up: Module Creation


5.2.2 Linking module libraries

In your working Makefile, you will refer to target libraries with a .la extension (Libtool archive). The .la extension replaces what would normally be .so, .dylib, .dll, or the native shared-object extension. Libtool provides a platform-independent abstraction of shared libraries, so the user need not worry about these details. The target library name need not be prefixed with ‘lib’, since it is being dlopened as a module (plug-in). Suppose the above source file was named foo.cc, and our target library is bar.la, one might write in the Makefile:

     # "Makefile" (continued)
     # list of dependent libraries (-l...)
     bar_la_LIBADD =
     
     # required flags
     bar_la_LDFLAGS = $(CHPSIM_MODULE_FLAGS)
     
     # -L search paths to dependent libraries
     # bar_la_LDFLAGS +=
     
     # object file list
     bar_la_OBJECTS = foo.lo
     
     bar.la: $(bar_la_OBJECTS)
             $(CXXLINK) $(bar_la_LDFLAGS) $(bar_la_OBJECTS) $(bar_la_LIBADD)

The bar.la to ‘bar_la’ name canonicalization is borrowed from Automake's variable naming convention. We've referenced some variables in the Makefile, defined in hackt-lt.mk:

— Makefile variable: CHPSIM_MODULE_FLAGS

Flags that tell Libtool to link the shared library to be suitable for dlopening (dynamic loading). Value should remain unmodified.

— Makefile variable: CXXLINK

The aggregate link command (without arguments).

Invokes hackt-libtool as a link wrapper. Should remain unmodified. Depends on the CXX Makefile variable.

— Makefile variable: CXX

The user should define the C++ compiler, which is also to be invoked as the linker. Autoconf users may wish to set this automatically through a configure script, e.g. CXX = @CXX@ in Makefile.in.

Other relevant variables are also provided:

— Makefile variable: HACKT_LIBTOOL

Defined to hackt-libtool, which is expected to be in the PATH. This is a renamed copy of the libtool script that was configured during the compilation of the tools. This has the advantage of storing and re-using all of the flags needed for building shared libraries on the host platform, thus saving the user from having to do any configure-detection when using chpsim.

— Makefile variable: HACKT_CONFIG

Defined to hackt-config, which is expected to be in the PATH. This script contains package installation information such as include header paths and libaries. For building chpsim modules, only a few compile time options are needed, no additional libraries are needed.

— Makefile variable: CPPFLAGS
— Makefile variable: CHPSIM_OBJECT_CPPFLAGS

Expands to flags needed to compile chpsim module source files. Gratuitously appled to all libtoolized compilations. CPPFLAGS may be appended by the user, but CHPSIM_OBJECT_CPPFLAGS may not.

— Makefile variable: CXXFLAGS

Intially empty, may be appended by the user for tuning compilation.

Summary: defining CXX suffices to successfully build chpsim module bar.la in the above example. One word of caution: the .la file is merely a placeholder that tells libtool where to find the actual built archives, which are actually built in the .libs subdirectory. Don't expect to be able to move these files arbitrarily without breaking. (There's still a good chance of it working because the libraries are not built for use in an installed location.)

Now you can build the module with ‘make bar.la’.