Re: Calling C dll from love2d
Posted: Thu Feb 06, 2020 3:06 am
I remember someone having a similar problem, here: https://love2d.org/forums/viewtopic.php?f=4&t=88104
Maybe this helps them.
Maybe this helps them.
Code: Select all
#include "lua/lua.h"
#include "lua/lauxlib.h"
int lua_summ(lua_State *L){
double a = luaL_checknumber(L, 1); /* get first arg */
double b = luaL_checknumber(L, 2); /* get second arg */
lua_pushnumber(L, a + b); /* returning value to lua */
return 1; /* count of args returned to lua */
}
#if defined(_WIN32) || defined(_WIN64) /* windows compat */
__declspec(dllexport)
#endif
int luaopen_mylib(lua_State *L) { /* compile it to mylib.dll or libmylib.so */
lua_newtable(L); /* create table */
lua_pushstring(L, "summ"); /* push "summ" as key*/
lua_pushcfunction(L, lua_summ); /* push lua_summ as value*/
lua_rawset(L, -3); /* assign key and value to this table (it gone from stack, but not table) */
return 1; /* return table to lua */
}
Code: Select all
local lib = require'mylib'
print(lib.summ(10, 20)) --> 30