0

I've been trying the hello world example of Luabind in Ubuntu, but I cannot make it work. Would anyone know how to make this example work in Ubuntu using g++?

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ cat hellobind.cpp 
#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;
}
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ g++ hellobind.cpp -I/usr/include/lua5.2/ -c -fPIC
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ g++ -shared -Wl,--whole-archive -o hellobind.so hellobind.o -lluabind -Wl,--no-whole-archive
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ cat test.lua 
require 'hellobind'
greet()
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ lua test.lua 
lua: error loading module 'hellobind' from file './hellobind.so':
    ./hellobind.so: undefined symbol: luaopen_hellobind
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    test.lua:3: in main chunk
    [C]: in ?
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ 

When I run the lua script, lua complains "undefined symbol: luaopen_hellobind".

My system details are as follows:

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ dpkg --get-selections | grep -v deinstall | egrep 'lua|boost'
libboost-date-time1.54.0:amd64          install
libboost-dev                    install
libboost-system1.54.0:amd64         install
libboost1.54-dev                install
libboost1.55-tools-dev              install
liblua5.2-0:amd64               install
liblua5.2-dev:amd64             install
libluabind-dev                  install
libluabind-examples             install
libluabind0.9.1                 install
lua5.2                      install
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ gcc --version
gcc (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ uname -a
Linux castor-ub 3.19.0-43-generic #49~14.04.1-Ubuntu SMP Thu Dec 31 15:44:49 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ 
hermit.crab
  • 852
  • 1
  • 8
  • 20

1 Answers1

1

I think you are looking at this example here, and they seem to load the shared library differently. Try a lua script like this:

package.loadlib('hellobind.so', 'init')()
greet()
Owen Delahoy
  • 806
  • 6
  • 22
  • Zekian, I tried your suggestion and I get this error: kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ cat test.lua loadlib('hellobind.so', 'init')() greet() kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ lua test.lua lua: test.lua:4: attempt to call global 'loadlib' (a nil value) stack traceback: test.lua:4: in main chunk [C]: in ? – hermit.crab Dec 18 '16 at 23:25
  • Ah it seems that in newer versions of lua, it is package.loadlib – Owen Delahoy Dec 19 '16 at 00:43
  • hi Zekian, unfortunately, it still does not work. thank you very much for helping though. `kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ cat test.lua package.loadlib('hellobind.so', 'init')() greet() kuyu@castor-ub:~/dkuyu/Dropbox/practice/lua/luabind$ lua test.lua lua: test.lua:1: attempt to call a nil value stack traceback: test.lua:1: in main chunk [C]: in ?` – hermit.crab Dec 19 '16 at 13:14
  • I compiled the lib on my computer with g++ 5.4, it worked fine. Perhaps your lib does not have an entry for `init` but rather something like `_init`. You can see the library's symbols with `nm -D hellobind.so`, check whether it contains `init`. – Owen Delahoy Dec 19 '16 at 15:07
  • thanks a lot for the tips, @Zekian. i was finally able to make it work by using `package.loadlib('./hellobind.so', 'init')()`. the `./` is important, as apparently, loadlib couldn't find the file hellobind.so. The following code also helped me in figuring out what was not working: `local initfunction, errormessage = package.loadlib('hellobind.so','init') if errormessage then print('Error loading hellobind:', errormessage) end` – hermit.crab Dec 19 '16 at 23:11