Lua Binding Generator Tutorial
A Lua binding is the interface between the Lua virtual machine and a native C function or a C++ method. It is the glue code that makes it possible to call C functions from within a Lua script. See our Online Lua Tutorial Server for a detailed introduction.
The idea is that you extend the Lua virtual machine to include your own (e.g.,device management) functions. Lua bindings can be designed manually or generated automatically. This web-application is designed for new users that want to include their proprietary functions and methods without having to learn how to manually construct the Lua bindings.
How to generate Lua bindings for C or C++
- Create a text file with the name functions.txt
- Copy all function declarations from your header file(s) and paste the declarations into functions.txt
- Navigate to the online Lua Binding Generator.
- Select a module name for your functions.
- Select C or C++ and click Continue.
- Use a browser that supports drag and drop such as Firefox or Chrome and drop the functions.txt file into the browser window.
- Download the generated C or C++ file.
- Integrate the generated C file with your build.
Note: Your function declarations pasted into functions.txt may depend on defines, types, enumerations, etc. Do not include these declarations in functions.txt. Instead, drop the header files, where these declarations are defined, into the browser. You can drag and drop multiple files at the same time. For example, select functions.txt and the header files with the declarations and drop these files into the browser. You cannot drop multiple times; all files needed must be dropped simultaneously.
Note: The generator will generate bindings for all functions found in your header files if you drag and drop header files into the browser window, but do not include functions.txt. The purpose with functions.txt is to limit the number of generated bindings to the functions you want to make available to Lua.
Note: functions.txt must include a copy of the class declarations and the class methods if you generate C++ code.
Note: You can ZIP all files together into one file and upload the file using the "traditional" upload button if you use a browser that does not support drag and drop.
Simplified Wrapper Generator (SWIG)
This service (the Lua Binding Generator Web Application) is using SWIG under the hood. Use this service as an initial stepping stone. Our recommendation is to learn how to manually create Lua bindings, but SWIG is a decent tool that can be used if you do not have the time to learn how to manually craft Lua bindings or if you have a large set of C/C++ functions you want to expose to Lua.
- Swig can be installed on Linux as follows: sudo apt install swig
- Windows binaries can be downloaded from the SWIG web site
- C++ Tutorial: Binding C++ to Lua Using Swig
- Linux Tutorial: Embedded Linux Web Interface Design
- Documentation: SWIG and Lua
- Documentation: The complete SWIG documentation
Note: The module interface file used by SWIG is automatically created by the Lua Binding Generator Web Application. The file is assembled as follows: