You can accomplish this by including the zstring source file then calling luaopen_zstring
after initializing Lua:
#include "zstring.c"
int main (){
L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L, luaopen_zstring);
lua_call(L, 0, 0);
luaL_dofile(L, "test.lua");
lua_close(L);
return 0;
}
It should be noted that even if you do not want to generate a shared library, you can still create an object file for zstring
(by using the -c
flag with gcc
, for example). You can then link that object file with your main source.
The steps for doing this are roughly:
- Compile
zstring.c
to an object file (e.g.gcc -c -o zstring.o zstring.c
) Create a header file named
zstring.h
:#ifndef ZSTRING_H #define ZSTRING_H int luaopen_zstring(lua_State *L); #endif
Include the header in your main source file:
#include "zstring.h" int main (){ L = luaL_newstate(); luaL_openlibs(L); lua_pushcfunction(L, luaopen_zstring); lua_call(L, 0, 0); luaL_dofile(L, "test.lua"); lua_close(L); return 0; }
- Compile the main file with the
zstring.o
object file linked (e.g.gcc -o myprogram main.c zstring.o
)