I have compiled the shared library for the "basic usage" example from the Luabind docs. However, I can't get it to call from Lua.
lbtest.cpp
extern "C"
{
#include "lua.h"
}
#include <iostream>
#include <luabind/luabind.hpp>
void greet()
{
std::cout << "hello world!\n";
}
extern "C" int init(lua_State* L)
{
using namespace luabind;
open(L);
module(L)
[
def("greet", &greet)
];
return 0;
}
This compiles to liblbtest.so. However, when I run the commands (as explained in this answer)
> lua
> package.loadlib('liblbtest.so', 'init')()
> greet()
I get this error:
stdin:1: attempt to call global 'greet' (a nil value) stack traceback: stdin:1: in main chunk [C]: ?
I have tried a few tests:
> fn, err = package.loadlib('liblbtest.so', 'init')
> print(fn)
nil
> fn, err = package.loadlib('liblbtest.so', 'init')()
stdin:1: attempt to call a nil value
stack traceback:
stdin:1: in main chunk
[C]: ?
> fn, err = package.loadlib('liblbtest.so', '_init')()
> print(fn)
nil
> fn, err = package.loadlib('liblbtest.so', '_init')
> print(fn)
function 0x1332e90
All of those loadlib
calls led to the same error in calling greet()
(the nil value as laid out earlier). It is interesting that the last one at least seems to return a function.
I am running Ubuntu 14.04 with Lua 5.1.5.
How do I get this to work?
UPDATE
I did manage to get a different error when I dropped the '.so' suffix and began using the require
syntax (as per this Lua mailing list conversation)
> require('liblbtest')
error loading module 'liblbtest' from file './liblbtest.so':
./liblbtest.so: undefined symbol: luaopen_liblbtest
stack traceback:
[C]: at 0x0047aff0
[C]: in function 'require'
stdin:1: in main chunk
[C]: at 0x00406670
However, using the nm -gC liblbtest.so
command I don't see that symbol exported. How do I make that happen?
Your C DLL does not follow format of lua C api DLL. Please try: __declspec(dllexport) int luaopen_liblbtest(lua_State* L) { lua_register(L, "init", init); return 1; }