c_swap
is called in lua as:lauxlib.h
and is safer; it will raise errors instead of segfaulting.return 2
. Lua will then know that top 2 values in the stack are the returned values.main.c
. main()
function illustrates how to run lua code from C.gcc main.c -o swap -llua5.2 -I/usr/include/lua5.2/
luaL_Reg
and a function luaopen_mylib
.main.c
. We will also add additional mysin
function.luaL_register(L, 'mylib', mylib);
instead of luaL_newlib(L, mylib);
.so
file withgcc main.c -shared -o mylib.so -fPIC -llua5.2 -I/usr/include/lua5.2/
luaL_checknumber
etc.). This is repetitive and can be a potential source for bugs.