5
votes

Lua tutorials all over the net show the use of lua_register() to expose the functions implemented in your extension DLL or so:

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

static int pushlua3(lua_State *L)
{
  lua_pushnumber(L, 3);
  return 1;
}

int luaopen_lua3pushbinder(lua_State *L) 
{
  lua_register(L,"pushlua3", pushlua3);
  return 0;
}

lua_register() is a macro and not a function, this is from the 5.2 manual:

http://www.lua.org/manual/5.2/manual.html#lua_register

[-0, +0, e]

void lua_register (lua_State *L,
               const char *name,
               lua_CFunction f);

Sets the C function f as the new value of global name. It is defined as a macro:

 #define lua_register(L,n,f) \
        (lua_pushcfunction(L, f), lua_setglobal(L, n))

if you use the functions separately, lua_pushcfunction is fine, but lua_setglobal crashes because it's trying to reference LUA_GLOBALSINDEX and that fails at runtime, not compile time.

So what is the right way to implement lua_register() now?

I would sort of have expected that when Lua moved to 5.2 and redid the concepts manifested with LUA_GLOBALSINDEX and thus lua_register() it would have been reasonable to change lua_register() so that it did it the 'new' way.

So, is there a header update that Ubuntu didn't pick up for lua5.2? should i have an include path that points to /usr/include/lua5.2 and then I wouldn't face this problem? I only have a Lua 5.1 include directory on my box.

tnx for any help you can provide.

1
Could You show an output of ldd ./your_app and how You're building this application, please? Also, which Ubuntu is that? NOTE: LuaDist virtual environments helps a lot messing with different Lua versions. - Kamiccolo
You need lua.h from Lua 5.2. - lhf
I now know that Ubuntu Software Center is an imperfect device. It had no listing for liblua5.2-0-dev, so i apt-get'ed it after doing some further reading. - JohnU
how do i mark this as solved? thanks for all of your collective help! - JohnU
@spaz you can add it as an answer to your question and accept it. (yes you can answer your own questions) - greatwolf

1 Answers

1
votes

The answer is that on Ubuntu 13.04 the Ubuntu Software Center informed me of the existence of lua5.2 but did not inform me of the existence of liblua5.2-0-dev.

I used apt-get to find it based on the suggestion for lhf that I needed the 5.2 headers.

My lua script calling a native dll/so demo went off just fine as a result