Let's assume we want to create an unnecessary custommath module for Lua.
localcustommath=require"custommath"print(custommath.add(10, 20))#ifndefCUSTOM_MATH_H#defineCUSTOM_MATH_H//c++ compatability#ifdef__cplusplus#defineC_API_BEGIN extern "C" {
#defineC_API_END }
#else#defineC_API_BEGIN#defineC_API_END#endif// !__cplusplusC_API_BEGIN#include<lua.h>#include<lualib.h>#include<lauxlib.h>#include<math.h>//the functions we want to implement. names do not matterintadd(lua_State*lua);
intsubtract(lua_State*lua);
intmultiply(lua_State*lua);
intdivide(lua_State*lua);
//the function which will be called when "require "custommath"" is called from luaint __declspec(dllexport) luaopen_custommath(lua_State*lua);
C_API_END#endif// !CUSTOM_MATH_HThe key part in this header file is this function:
int __declspec(dllexport) luaopen_custommath(lua_State*lua);First of all you need the _declspec(dllexport) because we want to create a shared library.
It is also required to have this specific signature int luaopen[MODULE](lua_State*) so Lua can find the function. In our case [MODULE] is custommath.
Note: The shared library has to have the same name as the module. In this case custommath.
I won't go into implementation details of the actual math functions. These are just basic Lua stack manipulations. If you are struggling with these please read the manual.
The int luaopen_custommath(lua_State*) implementation:
intluaopen_custommath(lua_State*lua)
{
//create table with 4 entrieslua_createtable(lua, 0, 4);
//set key value pairs of the tablelua_pushstring(lua, "add");
lua_pushcfunction(lua, &add);
lua_settable(lua, -3);
lua_pushstring(lua, "subtract");
lua_pushcfunction(lua, &subtract);
lua_settable(lua, -3);
lua_pushstring(lua, "multiply");
lua_pushcfunction(lua, &multiply);
lua_settable(lua, -3);
lua_pushstring(lua, "divide");
lua_pushcfunction(lua, ÷);
lua_settable(lua, -3);
//there should now be a table on the stack with our 4 functionsreturn1;
}Running the following Lua script:
localcustommath=require"custommath"locala=1.1localb=2.2print(tostring(a) .." + " ..tostring(b) .." = " ..tostring(custommath.add(a, b)))
print(tostring(a) .." - " ..tostring(b) .." = " ..tostring(custommath.subtract(a, b)))
print(tostring(a) .." * " ..tostring(b) .." = " ..tostring(custommath.multiply(a, b)))
print(tostring(a) .." / " ..tostring(b) .." = " ..tostring(custommath.divide(a, b)))
will result in following output:
1.1 + 2.2 = 3.3
1.1 - 2.2 = -1.1
1.1 * 2.2 = 2.42
1.1 / 2.2 = 0.5
Note: The Lua module can only be imported if the Lua version of the interpreter is compatible with the Lua version of the used API libraries.
This example builds a lua.exe and runs the test.lua script as post build process. The Lua versions are therefore exactly the same.
Note: You can create modules like this in C++ too. Just wrap a extern "C" around your code and especially around the lua includes like I did in the header file.
This project uses CMake to generate platform and compiler-specific build files.
Build just tested on Windows. Will most likely not work on other platforms. You had to atleast change the __declspec(dllexport) when you are using GCC to __attribute__((dllexport)) or __attribute__((visibility("default"))) when GCC version is greater equal than 4. But for simplicity I decided not to do it.
- Clone the repository with Lua submodule
git clone --recurse-submodules https://github.com/Seng3694/CLuaModuleExample - Generate the build files
mkdir bin cd bin cmake -G"Your Generator" ../CLuaModuleExample - Build the files
cmake --build . --config Release
If everything went correct you should see this after building:
1.1 + 2.2 = 3.3
1.1 - 2.2 = -1.1
1.1 * 2.2 = 2.42
1.1 / 2.2 = 0.5
If there are errors while building, remove the post_build custom command in the CMakeLists.txt. You have to run the lua executable on your own and pass the test.lua script file as an argument.