Luacxx
Lua binding library for modern C++
Install / Use
/learn @dafrito/LuacxxREADME
luacxx
Luacxx is a modern C++ library for binding C and C++ code to Lua.
It is designed to work with the Lua C API rather than hide it. You can freely mix raw Lua calls with Luacxx helpers for pushing values, reading values, constructing userdata, and exposing functions.
What It Focuses On
- Modern C++ and the standard library
- Direct interop with the Lua C API
- Low-overhead userdata and conversion support
- Extensible
Push<T>,Store<T>, andGet<T>customizations - CMake-first installation and consumption
Examples
The smallest useful example is just moving primitive and standard-library values across the Lua boundary:
#include <cassert>
#include <string>
#include <luacxx/algorithm.hpp>
#include <luacxx/convert/numeric.hpp>
#include <luacxx/convert/string.hpp>
#include <luacxx/load.hpp>
#include <luacxx/thread.hpp>
int main()
{
auto env = lua::create();
env["answer"] = 42;
env["name"] = std::string("luacxx");
lua::run_string(env, "assert(answer == 42)");
const auto answer = env["answer"].get<int>();
const auto name = env["name"].get<std::string>();
assert(answer == 42);
assert(name == "luacxx");
}
For a metatable-backed class binding, see the multi-file example in
examples/003-counter-binding:
examples/003-counter-binding/counter.hppdefines the C++ classexamples/003-counter-binding/counter_lua.hppdeclares the Luacxx metatable and binding entry pointsexamples/003-counter-binding/counter_lua.cppdefines the metatable methods and constructor bindingexamples/003-counter-binding/main.cppregisters the binding and exercises it from Lua
That example shows the usual “real object in userdata + explicit metatable” path that Luacxx uses for non-primitive types.
Build
Luacxx requires:
- C++20
- CMake 3.24 or newer
- Lua 5.2 or newer
Build and install:
cmake -S . -B build
cmake --build build
cmake --install build
To build the test suite:
cmake -S . -B build -DLUACXX_BUILD_TESTS=ON
cmake --build build --target luacxx_test_main
ctest --test-dir build --output-on-failure
Use From CMake
After installation, consume Luacxx with CMake:
find_package(luacxx CONFIG REQUIRED)
add_executable(my_app main.cpp)
target_link_libraries(my_app PRIVATE luacxx::luacxx)
The exported package also declares its Lua dependency.
Use From pkg-config
Luacxx also installs a pkg-config file:
pkg-config --cflags --libs luacxx
Conversions
Built-in conversions include:
- numbers and characters
std::string- C strings and
std::string_viewas push-only string inputs std::vectorstd::arraystd::mapstd::unordered_mapstd::optionalstd::tuplestd::shared_ptr
Additional types can be integrated by specializing the conversion templates.
If you want to add your own types, start with
docs/guide/conversion-templates.md.
Documentation
The user guide and reference live under docs/.
Start here:
docs/index.mdfor the full guide/reference indexdocs/guide/writing-bindings.mdfor the overall binding style and design advice
Common tasks:
docs/guide/handling-lua-errors.mdfor catching, inspecting, and throwinglua::errordocs/guide/loading-and-running-lua-code.mdforload_*andrun_*docs/guide/working-with-the-lua-stack.mdfor the basic Lua stack model and how Luacxx layers on top of itdocs/guide/iterating-stack-values.mdforlua::range<T>and homogeneous stack iterationdocs/guide/binding-a-primitive-type.mdfor teaching Luacxx a value-like typedocs/guide/binding-a-class.mdfor userdata-backed classesdocs/guide/working-with-shared-ptr.mdfor shared-ownership bindingsdocs/guide/working-with-strings.mdfor the intendedstd::stringpath and the raw Lua string escape hatchdocs/guide/working-with-callables.mdfor function pointers, member pointers,std::function, andlua::calldocs/guide/working-with-index.mdfor stack-slot access withlua::indexdocs/guide/working-with-global-and-link.mdfor symbolic access to globals, fields, and nested Lua pathsdocs/guide/working-with-reference.mdfor keeping Lua values alive across C++ calls with registry-backed referencesdocs/guide/working-with-lua-values.mdfor pushing Lua-specific values likenil, fresh tables, the globals table, and the registrydocs/guide/working-with-table-helpers.mdforlua::table::*,setfield, andsetglobaldocs/guide/standard-library-types.mdfor how built-instdtypes are represented, copied, and validateddocs/guide/light-userdata-and-void-p.mdfor whenvoid*works and why it is limited to light userdata
Concepts and policies:
docs/guide/conversion-templates.mdforPush<T>,Store<T>,Get<T>, andConstruct<T>docs/guide/metatable-macros.mdfor the quick metatable-definition macrosdocs/guide/luacxx-allow-missing-metatables.mdfor the fallback-metatable policy and why it is off by defaultdocs/guide/understanding-type-safety.mdfor how userdata type checks and inheritance casts workdocs/guide/understanding-userdata-memory.mdfor how Luacxx stores and destroys C++ objects in userdatadocs/guide/useful-lua-functions.mdfor the Lua C API functions Luacxx leans on most
Reference:
License
MIT
