sol2 is a C++ library binding to Lua. It currently supports all Lua versions 5.1+ (LuaJIT 2.0+ and MoonJIT included). sol2 aims to be easy to use and easy to add to a project. The library is header-only for easy integration with projects, and a single header can be used for drag-and-drop start up.
#include <sol/sol.hpp>
#include <cassert>
int main() {
sol::state lua;
int x = 0;
lua.set_function("beep", [&x]{ ++x; });
lua.script("beep()");
assert(x == 1);
}
#include <sol/sol.hpp>
#include <cassert>
struct vars {
int boop = 0;
};
int main() {
sol::state lua;
lua.new_usertype<vars>("vars", "boop", &vars::boop);
lua.script("beep = vars.new()\n"
"beep.boop = 1");
assert(lua.get<vars>("beep").boop == 1);
}
More examples are given in the examples directory here.
Find it here. A run-through kind of tutorial is here! The API documentation goes over most cases (particularly, the "api/usertype" and "api/table_proxy" and "api/function" sections) that should still get you off your feet and going, and there's an examples directory here as well.
Find the support option that's right for you, here! If you're happy to wait, you can just file a boring issue and we'll get to it Whenever There Is Time™.
You can find donation and sponorship options here and from the little heart button near the top of this repository that will take you to a bevy of links in which you can donate and show support for this project and others!
- Fastest in the land (see: sol2 bar in graph).
- Supports retrieval and setting of multiple types including:
eastl::string
,eastl::wstring
,eastl::u16string
andeastl::u32string
support (and for views).- understands and works with containers such as
eastl::map/unordered_map
, c-style arrays, vectors, non-standard custom containers and more. - user-defined types, with or without registering that type
eastl::unique_ptr
,eastl::shared_ptr
, and optional support of other pointer types likeboost::shared_ptr
.- custom
optional<T>
that works with references, and support for the inferioreastl::optional
. - C++17 support for variants and similar new types.
- Lambda, function, and member function bindings are supported.
- Intermediate type for checking if a variable exists.
- Simple API that completely abstracts away the C stack API, including
protected_function
with the ability to use an error-handling function. operator[]
-style manipulation of tables- C++ type representations in Lua userdata as
usertype
s with guaranteed cleanup. - Customization points to allow your C++ objects to be pushed and retrieved from Lua as multiple consecutive objects, or anything else you desire!
- Overloaded function calls:
my_function(1); my_function("Hello")
in the same Lua script route to different function calls based on parameters - Support for tables, nested tables, table iteration with
table.for_each
/begin()
andend()
iterators. - Zero string overhead for usertype function lookup.
sol2 makes use of C++17 features. GCC 7.x.x and Clang 3.9.x (with -std=c++1z
and appropriate standard library) or higher should be able to compile without problems. However, the officially supported and CI-tested compilers are:
- GCC 7.x.x+ (MinGW 7.x.x+)
- Clang 3.9.x+
- Visual Studio 2017 Community (Visual C++ 15.0)+
Please make sure you use the -std=c++2a
, -std=c++1z
, -std=c++17
or better standard flags
(some of these flags are the defaults in later versions of GCC, such as 7+ and better).
If you would like support for an older compiler (at the cost of some features), use the latest tagged sol2 branch. If you would like support for an even older compiler, feel free to contact me for a Custom Solution.
sol2 is checked by-hand for other platforms as well, including Android-based builds with GCC and iOS-based builds out of XCode with Apple-clang. It should work on both of these platforms, so long as you have the proper standards flags. If something doesn't work or you need special options, you may need to look into the different ways to support the project to have it done for you!
You can grab a single header (and the single forward header) out of the library here. For stable version, check the releases tab on GitHub for a provided single header file for maximum ease of use. A script called single.py
is provided in the repository if there's some bleeding edge change that hasn't been published on the releases page. You can run this script to create a single file version of the library so you can only include that part of it. Check single.py --help
for more info.
If you use CMake, you can also configure and generate a project that will generate the sol2_single_header
for you. You can also include the project using CMake. Run CMake for more details. Thanks @Nava2, @alkino, @mrgreywater and others for help with making the CMake build a reality.
Testing turns on certain CI-only variables in the CMake to test a myriad of configuration options. You can generate the tests by running CMake and configuring SOL2_TESTS
, SOL2_TESTS_SINGLE
, SOL2_TESTS_EXAMPLES
, and SOL2_EXAMPLES
to be on. Make sure SOL2_SINGLE
is also on.
You will need any flavor of python3 and an available compiler. The testing suite will build its own version of Lua and LuaJIT, so you do not have to provide one (you may provide one with the LUA_LOCAL_DIR
variable).
"A Sun For the Moon - A Zero-Overhead Lua Abstraction using C++"
ThePhD
Lua Workshop 2016 - Mashape, San Francisco, CA
Deck
"Wrapping Lua C in C++ - Efficiently, Nicely, and with a Touch of Magic"
ThePhD
Boston C++ Meetup November 2017 - CiC (Milk Street), Boston, MA
Deck
"Biting the CMake Bullet"
ThePhD
Boston C++ Meetup February 2018 - CiC (Main Street), Cambridge, MA
Deck
"Compile Fast, Run Faster, Scale Forever: A look into the sol2 Library"
ThePhD
C++Now 2018 - Hudson Commons, Aspen Physics Center, Aspen, Colorado
Deck
"Scripting at the Speed of Thought: Using Lua in C++ with sol2"
ThePhD
CppCon 2018 - 404 Keystone, Meydenbauer Center, Aspen, Colorado
Deck
"The Plan for Tomorrow: Compile-Time Extension Points in C++" ThePhD C++Now 2019 - Flug Auditorium, Aspen Physics Center, Aspen, Colorado Deck
sol2 is distributed with an MIT License. You can see LICENSE.txt for more info.
If you need a custom solution, feel free to reach out.