2

I am trying to create a C module to be called from a lua script. I am working on debian linux. I am using mysql-proxy and lua 5.2. I have created (copied from a tutorial) some example functions to be called.

The loader is defined like this:

int luaopen_luacall(lua_State* l)
{

    luaL_newlibtable(l, luacall);
    luaL_setfuncs(l, luacall, 0);
    return 1;
}

To call this from lua I use this code:

luacall = require("luacall")
local f = luacall.fun1()

I have compiled it with this command:

g++ -shared -Wl,-E,-soname,libluacall.so -o luacall.so luacall.c  -fPIC -llua -ldl

When I try to run the script I get the following error on the require command:

 error loading module 'luacall' from file '/usr/lib/mysql-proxy/lua/luacall.so':
        /usr/lib/mysql-proxy/lua/luacall.so: undefined symbol: luaL_setfuncs

I am really lost on what I am doing wrong.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
jordi
  • 1,157
  • 1
  • 13
  • 37
  • 4
    Are you definitely running Lua 5.2? I don't mean the library that you link to in g++, I mean where you run the script that does the `require`? – Alex Jan 23 '14 at 13:11
  • I have uninstalled Lua 5.1. The only LUA binary I have on the system is Lua 5.2. I am not quite sure what or how is calling mysql-proxy. I have tried to include the command print`("VERSION ", lua_version())` but I get an error `attempt to call global 'lua_version' (a nil value)' – jordi Jan 23 '14 at 14:34
  • What does `lua -v` print? – lhf Jan 23 '14 at 15:48
  • What is luacall array defined as in C++? – Oliver Jan 23 '14 at 16:01
  • Lua 5.2.1 Copyright (C) 1994-2012 Lua.org, PUC-Rio – jordi Jan 23 '14 at 16:35
  • 1
    luacall is the name of the library I want to call. I am not sure of understanding the array question. – jordi Jan 23 '14 at 16:38
  • http://lua.2524044.n2.nabble.com/require-so-and-call-may-cause-abort-trap-at-free-td7170147.html According to this you shouldn't link with -llua when compiling shared libraries, have you seen this thread? – mpeterv Jan 23 '14 at 21:04
  • -llua was a later adition, but without it I get the same result. I have tried the "-undefined dynamic_lookup" asi recomended in the post and get a non existent file error. What I don't understand is why i get the error on the "luaL_setfuncs" but not on the previous "luaL_newlibtable". – jordi Jan 24 '14 at 08:03

2 Answers2

5

Never use -llua when building Lua modules. The Lua interpreter itself is already linked with liblua and satisfies those symbols when the module is loaded. Linking your module against liblua is clashing with the interpreter.

Craig
  • 51
  • 2
3

I think I found the problem (not yet the solution): Mysql-proxy runs internally an embended lua library.

mysql-proxy -V

gives as result

mysql-proxy 0.8.1
  chassis: mysql-proxy 0.8.1
  glib2: 2.30.2
  libevent: 2.0.19-stable
  LUA: Lua 5.1.4
    package.path: /usr/lib/mysql-proxy/lua/?.lua
    package.cpath: /usr/lib/mysql-proxy/lua/?.so
-- modules
  admin: 0.8.1
  proxy: 0.8.1

So I am running the wrong lua version. I think that this explains the luaL_setfuncs error. I have seen that even the 0.8.4 version includes this version of lua, so I will have to rewrite the C library.

the final code of the module ends like this (and runs!!!):

static const struct luaL_Reg my_luacall[] = {
    {"trasnquery", trasnquery},
    {"fun2", function_2},
    {NULL, NULL}
};

int luaopen_luacall(lua_State* l)
{
    luaL_register(l, "luacall", my_luacall);
    return 1;
}
jordi
  • 1,157
  • 1
  • 13
  • 37