无法将共享库(与C ++和Luabind一起编译)包加载到Lua中 [英] Can't load shared library (compiled with C++ and Luabind) package into Lua
问题描述
我已经为基本用法"示例编译了共享库来自Luabind文档.但是,我无法从Lua拨打电话.
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;
}
这将编译为liblbtest.so.但是,当我运行命令时(如此答案中所述)
This compiles to liblbtest.so. However, when I run the commands (as explained in this answer)
> lua
> package.loadlib('liblbtest.so', 'init')()
> greet()
我收到此错误:
stdin:1:尝试调用全局"greet"(nil值)堆栈回溯: stdin:1:在主块[C]中:?
stdin:1: attempt to call global 'greet' (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')()
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
所有这些loadlib
调用在调用greet()
时都导致了相同的错误(如前所述,nil值).有趣的是,最后一个似乎至少返回了一个函数.
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.
我正在使用Lua 5.1.5运行Ubuntu 14.04.
I am running Ubuntu 14.04 with Lua 5.1.5.
如何使它正常工作?
更新
当我删除'.so'后缀并开始使用require
语法时,确实设法得到了一个不同的错误(按照
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
但是,使用nm -gC liblbtest.so
命令我看不到该符号的导出.我如何做到这一点?
However, using the nm -gC liblbtest.so
command I don't see that symbol exported. How do I make that happen?
推荐答案
您的C DLL不遵循lua C api DLL的格式.请试试: __declspec(dllexport)int luaopen_liblbtest(lua_State * L) { lua_register(L,"init",init); 返回1; }
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; }
这篇关于无法将共享库(与C ++和Luabind一起编译)包加载到Lua中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!