Implementing standalone C++ modules
So far in this book, we have only explicitly registered C++ modules to Lua in C++ code. However, there is another way to provide a C++ module to Lua.
You can produce a shared library for a module and place it in Lua’s search path. When the Lua code requires the module, Lua will load the shared library automatically.
By reusing our Destinations
class, this is simple to implement. Create a file named DestinationsModule.cpp
and fill it exactly as follows:
#include "Destinations.h" #include "LuaModuleExporter.hpp" #include <lua.hpp> namespace { LuaModuleExporter module = LuaModuleExporter<Destinations>::make( DestinationsLuaModuleDef::def); } extern "C" { int luaopen_destinations(lua_State *L) { lua_createtable(L, 0, module.luaRegs...