Hello World Using Lua Api


/ Published in: C
Save to your folder(s)

This is a hello world and the first of a series that shows how to embed Lua in a C/C++ application.


Copy this code and paste it in your HTML
  1. #include "lua.h"
  2. #include "lualib.h"
  3. #include "lauxlib.h"
  4.  
  5. int main()
  6. {
  7. // Create a context. This stores all variables and function available to Lua.
  8. lua_State * execContext = luaL_newstate();
  9. // Adding Lua basic library
  10. luaL_openlibs(execContext);
  11.  
  12. // Pushing the print function to the stack
  13. lua_getfield(execContext, LUA_GLOBALSINDEX, "print");
  14. // Adding the first argument to the print function
  15. lua_pushstring(execContext,"Hello World !");
  16. // print "Hello World !"
  17. // the state, number of args, number of returned values
  18. lua_call(execContext,1,0);
  19.  
  20. // Cleaning the house
  21. lua_close(execContext);
  22. }

URL: http://coderep.blogspot.com/2008/11/embedding-lua-in-cc-application-part-1.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.