Alright, so after preparing Lua, here are a few small first step on how to make use of this language within a C++ context
By the way: I haven’t yet used Lua1 very much, but I’ve read a few tutorials: Well, I don’t think that I will ever fall in love with that language (although I also though that about Powershell many years ago, and that has changed quite a bit in recent years!): Some decisions reading syntax and grammar are pretty… interesting :-/
But I also looked thru the internet for alternatives; any other languages (since 1993, when Lua appeared on the scene), that are similarly small, performant and embeddable in C or C++, resp. for extending C or C++:
And it doesn’t really look that way. There have been a few attempts (mostly based on Lua), but they either were even more niche and/or withered away (after a few years/several years ago).
Something like Python is way more popular and widespread, but looks also to be too big and too powerful.
Setting up the CMake build system
Well, nowadays I have my own helper functions/scripts to quickly set up a prefabricated CMake project for Windows/Visual Studio, by some “boilerplate code” (project template), so I won’t go into the details (this posting is, as many posts here, primarily written for my future self; and explaining that all here in details would just distract from the actual topic).
I only needed to add a few paths in the CMakeLists.txt file, so that the header files and the static library for Lua
could be found by the C++ compiler and linker.
(For this test, these are only hardwired paths; don’t pay too much attention.)
target_include_directories (${PROJECT_NAME}
# ...
PRIVATE
"C:/Program Files/Lua/src"
# ...
)
target_link_libraries (${PROJECT_NAME}
PRIVATE
"C:/Program Files/Lua/lua-static-${CPU_ARCHITECTURE}.lib"
)
Source Code
The host program, written in C++, will just call a Lua script that simply prints a line of text:
-- script.lua
print("Hello, world... from a Lua script file!")
The host, the C++ code, sets up the Lua environment (plus a few more necessary steps) and then calls an external Lua script (provided as a path via a command line argument). This is really just a bare “Minimum Viable Product” for now:
#include <iostream>
#include <format> // I really like the new format strings, but it requires support for the ISO C++ 20 standard!
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h> // The auxiliary library is incuded with Lua and offers a few convenience functions.
}
int main (int argc, char* argv[])
{
if (argc != 2)
{
std::cout << "Must be called with exactly one argument: The full path to a *.lua file!" << std::endl;
return 1;
}
const auto L = luaL_newstate();
// Set up a new Lua state.
luaopen_base(L);
// Open Lua's "base" standard library, to make print() available for the Lua script.
int status = luaL_dofile (L, argv[1]);
// Load (and immediately run!) the Lua script from the provided path.
if (status != 0)
std::cout << std::format("luaL_dofile failed: {0}", lua_tostring(L, -1)) << std::endl;
lua_close(L);
// Close the Lua state again and clean up.
return 0;
}
Film & Television (55)
How To (64)
Journal (17)
Miscellaneous (4)
News & Announcements (21)
On Software (12)
Projects (26)