From efa24b220d9633d5d7bfef632b33df180dcb0e74 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Thu, 27 May 2021 21:10:45 +0100 Subject: Update 10 html to md articles --- md/writeup/embedding_lua_in_c.md | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 md/writeup/embedding_lua_in_c.md (limited to 'md/writeup/embedding_lua_in_c.md') diff --git a/md/writeup/embedding_lua_in_c.md b/md/writeup/embedding_lua_in_c.md new file mode 100644 index 0000000..ee875ae --- /dev/null +++ b/md/writeup/embedding_lua_in_c.md @@ -0,0 +1,71 @@ +title:Embedding Lua in C +keywords:lua,c,embed + +# Embedding Lua in C +Bedimming lua in you C programs is can be done in few minutes. +Many examples is in lua-users.org. + +First thing to write is module and then compile everything with lua precompiled lib. + +```c +int module_register(lua_State*); +void module_print(lua_State*); +int module_getone(lua_State*); + +int module_gc(lua_State*); +int module_tostring(lua_State*); + +static const luaL_reg module_methods[] = +{ + //{,(void *)}, + {"print", (void *)module_print}, + {"getone", (void *)module_getone}, + {0, 0} +}; + +static const luaL_reg module_meta[] = +{ + {"__gc", (void *)module_gc}, + {"__tostring", (void *)module_tostring}, + {0, 0} +}; +``` + + +to make printf("%s\n") available in lua + +```c +void module_print( lua_State *L) +{ + int argc = lua_gettop(L); + int n; + for (n=1; n <= argc; n++) printf("%s\n", lua_tostring(L, n)); +} +``` + + +next one function that have return value 1 + +```c +int module_getone(lua_State *L) +{ + int x=1; + lua_pushnumber(L, x); + return 1; +} +``` + + +and easy to compile if needed. + +``` +gcc -c module.c +gcc module.o main.c -o main -llua +``` + +## Links +http://lua-users.org/wiki/SampleCode + +## Downloads +lua_embed.zip - +2KiB - http://archive.main.lv/files/writeup/embeding_lua_in_c/lua_embed.zip \ No newline at end of file -- cgit v1.2.3