This material is covered in the Lua documentation, but I will do my own version here to help people who are relying on this site for Lua information. :)
Let's start with a simple C program that creates a Lua "script space" and runs a simple literal script:
This has the absolute basics. It:
I needed the lua.h file from the Lua distribution, and the lua50.lib file from the Windows binary distribution. The file lua50.dll also needs to be somewhere in your path.
I compiled it like this (under Cygwin):
This compiled test.c (the above file) and linked in the lua50.lib library. Then running it gives the expected results:
Let's start with a simple C program that creates a Lua "script space" and runs a simple literal script:
#include "lua.h"
const char * myscript =
" print ('Hello, world. Lua version is', _VERSION)";
int main (void)
{
lua_State * L = lua_open(); /* opens Lua */
luaopen_base(L); /* opens the basic library */
luaL_loadbuffer (L, myscript, strlen (myscript), "example"); /* compile */
lua_pcall (L, 0, 0, 0); /* execute */
lua_close (L); /* close Lua */
return 0;
} /* end of main */
This has the absolute basics. It:
- Creates a script space (in the variable L)
- Adds to it the basic library functions (such as 'print')
- Compiles the script (with luaL_loadbuffer)
- Executes the script (with lua_pcall)
- Closes the script space
- Exits
I needed the lua.h file from the Lua distribution, and the lua50.lib file from the Windows binary distribution. The file lua50.dll also needs to be somewhere in your path.
I compiled it like this (under Cygwin):
gcc test.c lua50.lib -o test.exe
This compiled test.c (the above file) and linked in the lua50.lib library. Then running it gives the expected results:
Hello, world. Lua version is Lua 5.0.2