diff options
author | FreeArtMan <dos21h@gmail.com> | 2021-05-27 21:10:45 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2021-05-27 21:10:45 +0100 |
commit | efa24b220d9633d5d7bfef632b33df180dcb0e74 (patch) | |
tree | ac8502acf0116fbbb42a09c6956be70b9a3fc49f /md/writeup/embedding_lua_in_c.md | |
parent | e63ed8a651e5246f8698a9c1c3e540029710d0e9 (diff) | |
download | md-content-efa24b220d9633d5d7bfef632b33df180dcb0e74.tar.gz md-content-efa24b220d9633d5d7bfef632b33df180dcb0e74.zip |
Update 10 html to md articles
Diffstat (limited to 'md/writeup/embedding_lua_in_c.md')
-rw-r--r-- | md/writeup/embedding_lua_in_c.md | 71 |
1 files changed, 71 insertions, 0 deletions
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 |