lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Did someone know a better way to typecast a const char * (that is about
lua_tostring(L, n)) into a char * then vector or c_str()? I'm fighting with

About the Lua 5.1.2... well, I must say that im really stupid. For future
posts from someone interested, here comes:

/*File whatever.cpp*/
extern "C"
   #include <lua.h>
   #include <lauxlib.h>
   #include <lualib.h>
#include <iostream>

"whatever_type" "whatever_your_function_name_have"(lua_State *L) 
        int argc = lua_gettop(L); /*the function arguments came from here.
First time, i just cant figure out that*/
        return "whatever_type";
int main(int argc, char* argv[])
        int ret;
        const char *filename;
        const char *returndeal;
        filename = argv[1];
        lua_State *L;
        L = lua_open();
        luaL_openlibs(L); //Load all resources from the lua env. To see what
is loading, go to lualib.h
        lua_register(L, "serial", serial);
        //This is how to pass a table to lua. The table name will be...
        lua_pushnumber( L, 1 );
        lua_pushstring( L, returndeal);
        lua_rawset( L, -3 );
        lua_setglobal(L, "arg" );
        //... arg!!!
        int result = 0;
        ret = luaL_loadfile(L,filename); /*here you load the file. You can
do that in a different manner to pick up the args directly fom the prompt
command line*/
        if (ret == 0)
            result = lua_pcall( L, 0, LUA_MULTRET, 0 ); //This line will
execute the script
            std::cout << "bad" << std::endl;/*and this one will tell you,
iff appears, that you must study the lua script manual again.*/

        return 0;
/*Eof whatever.cpp*/

There are many ways to build lua embedded in your application. To do so in a
very easy (and static) way, do as this follow instructions (for Lua 5.1.2):

Building lualib

/*LuaLib.c - remember to set include directory to your lua/src directory*/
#define lualib_c
#include <unistd.h> 
#define LUA_TMPNAMBUFSIZE       32 
#define lua_tmpnam(b,e)         { e = (tmpnam(b) == NULL); } 

#include "lauxlib.c"
#include "lbaselib.c"
#include "ldblib.c"
#include "liolib.c"
#include "linit.c"
#include "lmathlib.c"
#include "loadlib.c"
#include "loslib.c"
#include "lstrlib.c"
#include "ltablib.c"

/*Eof Lualib.c*/

Build this one, whatever name you like, as a static library. So with this
other file:

/*LuaCore - remember to set include directory to your lua/src directory */

#define luacore_c

#include "lapi.c"
#include "lcode.c"
#include "ldebug.c"
#include "ldo.c"
#include "ldump.c"
#include "lfunc.c"
#include "lgc.c"
#include "llex.c"
#include "lmem.c"
#include "lobject.c"
#include "lopcodes.c"
#include "lparser.c"
#include "lstate.c"
#include "lstring.c"
#include "ltable.c"
#include "ltm.c"
#include "lundump.c"
#include "lvm.c"
#include "lzio.c"

/*Eof LuaCore.c*/

Include the 2 libs (remember to set include directory to your lua/src
directory) in the project where you will build the very first example in
this email and Voila!!! You get yourself this thing done!!