btbytes (owner)
Revisions
-






430ec5
btbytes
Sat Jul 11 07:46:09 -0700 2009
-






a4a69f
btbytes
Sat Jul 11 07:45:41 -0700 2009
-






28920d
btbytes
Sat Jul 11 07:44:30 -0700 2009
-






d62a14
btbytes
Sun Jun 28 21:37:25 -0700 2009
-






6be837
btbytes
Sun Jun 28 21:28:22 -0700 2009
-






ccfed4
btbytes
Sun Jun 28 21:28:02 -0700 2009
-






ae1231
btbytes
Sun Jun 28 21:02:08 -0700 2009
-






1f7c69
btbytes
Sun Jun 28 20:48:04 -0700 2009
-






163a7e
btbytes
Sun Jun 28 20:47:46 -0700 2009
This gist is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are given this private URL.
Every gist with this icon (
) is private.
Every gist with this icon (
This gist is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
Getting started with Lua and C++
Step 1: Install Lua
$ sudo apt-get install libreadline5-dev
$ sudo apt-get install lua-5.1 liblua5.1-0-dev
Step 2: Test C++ and Lua
/*
moon1.cpp
*/
#include
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
void report_errors(lua_State *L, int status)
{
if ( status!=0 ) {
std::cerr << "-- " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1); // remove error message
}
}
int main(int argc, char** argv)
{
for ( int n=1; n<< "-- Loading file: " << file << std::endl;
int s = luaL_loadfile(L, file);
if ( s==0 ) {
// execute Lua program
s = lua_pcall(L, 0, LUA_MULTRET, 0);
}
report_errors(L, s);
lua_close(L);
std::cerr << std::endl;
}
return 0;
}
The above code runs on Lua 5.1. There is an important change in Lua 5.1 API which requires the luaopen_* functions to be called through Lua, like Lua functions. See luaL_openlibs(L); line.
Compile
$ gcc moon1.cpp -o moon1 `pkg-config --cflags --libs lua5.1`
Create a simple Lua script
-- hello.lua
print ("Hello Lua")
Run the Lua script via the CPP program.
$ ./moon1 hello.lua
-- Loading file: hello.lua
Hello Lua


