Building C/C++ Modules

The Mako Server's Lua bindings (the Lua to C or C++ API) can be extended. Shared libraries (DLLs on Windows) can be dynamically loaded by Lua scripts at runtime. Loading shared libraries at runtime is in Lua terminology called loading a module and is performed by calling the function require("module-name") from a Lua script or LSP page. Building C modules is an option available to developers with C experience.

Mako Server loading external DLLs

A Lua C module makes it possible to extend the server's API with new functions. For example, you can add new Lua functions that access your serial or USB driver and that make it possible to manage hardware via a web based user interface. Writing modules require that you learn how to design Lua bindings or that you learn how to use our online Lua Binding Generator.

Building Lua modules is described in the Lua book and you can find an older version of the book on the Lua site. The Lua wiki also provides a number of tutorials. Lua bindings can be created manually or by using our Automatic Lua Binding Generator. The Lua binding generator is great for new developers that want to fast track the Lua Binding generation for their C APIs, but eventually you should learn how to code these bindings manually, especially if you have a complex C/C++ API.

Mako Lua Binding

Building modules for the Mako Server is identical to building modules for standard Lua with the exception that you do not link the module with the standard Lua DLL/shared-library. The Mako Server is designed with the purpose in mind of providing most users with all functionality required in one executable file. The Lua virtual machine is integrated into the Mako Server executable, so it is therefore not possible to link with the standard Lua DLL/shared-library. Instead of linking with the Lua DLL, you compile and link your code with our Lua API wrapper. The wrapper code extracts all Lua functions from the Mako Server executable. The development sequence is identical to the standard Lua binding development sequence with the exception that you must initialize the wrapper API by calling one function at startup of your DLL/shared library.

The online Lua tutorial includes an example that lets you compile a ready to use C module and load the module at runtime.

Download the Mako Server Module Example

  1. Download the Module Example
  2. Unpack the ZIP file
  3. Open and read the README.txt file

Note: the automatic Lua binding generator is compatible with the Mako Server's Lua API wrapper.

Examples: MongoDB and PostgreSQL Modules

(Creating Mako Server Modules using Existing Lua Modules)

You may use existing Lua Modules and port them to the Mako Server. There are three types of modules: Lua modules, C/C++ (Lua Binding) Modules, and hybrid modules that include both Lua and C code. All modules are loaded by using the Lua function "require".

local mymodule = require "mymodule"

For the above, Lua will look for a module called mymodule.lua and (.dll for Windows). If it finds the module, the module is loaded.

Standard C modules can be ported to Lua manually or by creating a script. We have prepared two scripts that are designed to build a MongoDB Module and a PostgreSQL Module. The two scripts, which are designed for Linux, completely automates the process of building/compiling the modules. Build the two modules as follows:


See the following PostgreSQL tutorial for details: Using Lua to Access a PostgreSQL Database.

tar xvzf MongoDB.tar.gz
cd MongoDB