lua-users home
lua-l archive

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


It's a longshot, but I notice you haven´t loaded the 'package' library, which probably will be needed for the 'require' function to work.

On 12/18/07, Rodrigo Araújo <alf.rodrigo@gmail.com> wrote:
Hi there,

I'm using Lua and C. The idea is that a C program executes a .lua script and i'm having trouble using require statements inside that script.
I've defined two .lua files, the first one (named rf.lua) has only one function name teste(). The other one (named hello.lua) imports the file rf.lua (require "rf") and calls print(rf.teste()).

Calling it through the lua interpreter goes well, but when I write a program in C that runs the file hello.lua it doesn't output anything.

I'm pasting the codes here.

Thanks in advance,

Rodrigo Araújo


------------ File rf.lua ------------

  1 module(..., package.seeall);
  2
  3 function teste(x)
  4     return x*x + 2
  5 end

------------ File hello.lua ------------

  1 require "rf"
  2
  3 print ("HELLO WORLD")
  4 print (math.sqrt(4))
  5 print (rf.teste(2))

------------ File call.cpp ------------

  1 extern "C"{
  2     #include "lua.h"
  3     #include "lauxlib.h"
  4     #include "lualib.h "
  5 }
  6
  7 #include <stdio.h>
  8
  9 static const luaL_reg lualibs[] =
 10 {
 11     {"base", luaopen_base},
 12     {"table", luaopen_table},
 13     {"string", luaopen_string},
 14     {"math", luaopen_math},
 15     {"debug", luaopen_debug},
 16     {"rf", NULL}
 17 };
 18
 19 static void openstdlibs( lua_State *l )
 20 {
 21     const luaL_reg *lib = lualibs;
 22     for (; lib->func; lib++)
 23     {
 24         lib->func(l);
 25         lua_settop(l, 0);
 26     }
 27 }
 28
 29 int main (int argc, char *argv[]){
 30     lua_State *state = lua_open();
 31     openstdlibs( state );
 32     luaL_dofile(state,"hello.lua");
 33     return 0;
 34 }

It's compiled through g++ -o call call.cpp -llua5.1



--
Luís Eduardo Jason Santos