Software Side Roads

Scenic Detours Along the Way to Release


Embedding Lua in C/C++

Lua is a scripting language with simple syntax and very fast execution time.  It may be easily embedded in a variety of environments including C/C++ applications.

There are many general tutorials geared towards the general aspects of embedding Lua. This pair of examples is directed towards a specific application need in which multiple scripts may be run based on application events and information should be returned to the parent applications and/or other scripts for processing.

It was not easy for me to find examples which incorporated the following elements:

  1. cross platform including Windows
  2. full worked sample code, not just excerpts showing the use of functions
  3. reasonably detailed handling of edge cases including type errors and things like ‘nil’ as values

This is the first in a pair of essays which address, in order, how to embed Lua scripting in such an application and then how to pass data back and forth between the Lua and application environments. The second article expands to include pass data and function calls between Lua and c++. The source in the form of Visual Studio projects for both articles is available in a single repository at https://github.com/SoftwareSideRoads/embedluacplusplus

In order to keep things as simple as possible yet still maintain Lua as a separate library, we will:

  1. Create a basic solution and then C++ windows console application in Visual Studio 2022
  2. Create a static C/C++ library project in Visual Studio which will be the Lua library
  3. Create C and C++ applications which load a simple script from a string and run it.
  4. Add a reference to the Lua library from the applications

Tne C++ code can be found within the repository at basic embedding of Lua in C++. The corresponding example without C++ is available at basic embedding of lua in C. Because Lua is written in C, the level of power available to the parent application is basically the same whether the host application is C or C++.

Once the project structure is established, the minimum calls to run a Lua script within the parent application are quite simple:

  • lua_openstring()
  • lua_openlibs()
  • lua_loadbuffer()
  • lua_pcall()
  • lua_close()

Without further connections between the application and scripting environment, there would not generally be much of a point to doing this as the same functional result could be obtained by simply executing a Lua interpreter on the script.

The followup article will add a project which demonstrates how to maintain state for a particular script between runs and how to pass data back to the application from the script.



Leave a Reply

About Me

The focus of my work is writing software tools in support of engineering and financial analysis plus taking care of the source and results data for quality and documentation purposes. Most of my professional time has been spent in the engineering, particularly naval architecture, and finance sectors. Many side tasks which aren’t strictly central to my deliverables crop up and must be dealt with. These posts are notes on the little side tours and cul-de-sacs along the way.

Newsletter

Discover more from Software Side Roads

Subscribe now to keep reading and get access to the full archive.

Continue reading