【问题标题】:How make luabind work in ubuntu?如何让 luabind 在 ubuntu 中工作?
【发布时间】:2017-05-03 14:04:11
【问题描述】:

我一直在尝试 Ubuntu 中的 hello world 示例 Luabind,但我无法使其工作。有人知道如何使用 g++ 使这个示例在 Ubuntu 中工作吗?

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$ 

当我运行 lua 脚本时,lua 报错“未定义符号:luaopen_hellobind”。

我的系统详情如下:

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$ 

【问题讨论】:

    标签: c++ ubuntu lua luabind


    【解决方案1】:

    我认为您正在查看此示例here,并且它们似乎以不同的方式加载共享库。试试这样的 lua 脚本:

    package.loadlib('hellobind.so', 'init')()
    greet()
    

    【讨论】:

    • Zekian,我尝试了你的建议,我得到了这个错误: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: 尝试调用全局 'loadlib' (一个 nil 值) 堆栈回溯:test.lua:4: in main chunk [C]: in ?
    • 啊好像在新版本的lua中是package.loadlib
    • 嗨 Zekian,很遗憾,它仍然无法正常工作。非常感谢你的帮助。 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 ?
    • 我在我的电脑上用 g++ 5.4 编译了这个库,它运行良好。也许您的库没有init 的条目,而是类似_init 的条目。 nm -D hellobind.so可以看到库的符号,检查是否包含init
    • 非常感谢@Zekian 的提示。我终于能够通过使用package.loadlib('./hellobind.so', 'init')() 使其工作。 ./ 很重要,很明显,loadlib 找不到文件 hellobind.so。下面的代码还帮助我找出了什么不起作用:local initfunction, errormessage = package.loadlib('hellobind.so','init') if errormessage then print('Error loading hellobind:', errormessage) end
    猜你喜欢
    • 2013-01-30
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多