- Notifications
You must be signed in to change notification settings - Fork1
Example for creating a Lua module in C
License
Seng3694/CLuaModuleExample
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Let's assume we want to create an unnecessarycustommath 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_H
The 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 signatureint luaopen[MODULE](lua_State*) so Lua can find the function. In our case[MODULE] iscustommath.
Note: The shared library has to have the same name as the module. In this casecustommath.
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.
Theint 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.31.1 - 2.2 = -1.11.1 * 2.2 = 2.421.1 / 2.2 = 0.5Note: 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 alua.exe and runs thetest.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 aextern "C" around your code and especially around the lua includes like I did in the header file.
This project usesCMake 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 bincd bincmake -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.31.1 - 2.2 = -1.11.1 * 2.2 = 2.421.1 / 2.2 = 0.5If there are errors while building, remove thepost_build custom command in theCMakeLists.txt. You have to run thelua executable on your own and pass thetest.lua script file as an argument.
About
Example for creating a Lua module in C
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.