Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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); /* return function value */
- 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 mylib.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 */
- }
- /*
- -- compile command like this:
- -- tcc -shared -L./path_to_lua.lib -I./path_to_lua.h -llua51 main.c -o ./mylib.dll
- -- Usage on lua-side:
- local lib = require'mylib'
- print(lib.summ(10, 20)) --> 30
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement