Lua 脚本调用 C 共享库的最佳方式? [英] Best way for Lua script to call a C shared lib?

查看:31
本文介绍了Lua 脚本调用 C 共享库的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更多的是 Python 和 CTYPES 背景......并且正在尝试找出从 Lua 脚本调用标准 C 共享库的最佳方法.

I come from more of a Python and CTYPES background...and am trying to work out the best way to call a standard C shared lib from Lua script.

我听说过外星人"(http://alien.luaforge.net/),但不确定它的流行程度?意味着它会定期更新吗?

I have heard of "Alien" (http://alien.luaforge.net/), but am not sure how current it is? Meaning it is updated regulary?

例如,我知道这是一个 SQLite Lua 库,但假设我想调用在编译 SQLIte 时创建的 SQLite 共享库?

For example, I know that thier is an SQLite Lua library but let's say I wanted to call the SQLite shared lib that is created when I compile SQLIte?

例如:在我的情况下,lib 位于/usr/local/lib/libsqlite3.so

So for example: In my case the lib is in /usr/local/lib/libsqlite3.so

如果我写一个小脚本只是为了看看它是否可以加载lib

If I write a little dummy script just to see if it can load the lib

require "libsqlite3"

print "hello"

我收到以下错误:

debian@debian:~/Desktop/SQLite Test$ lua sqlite_test.lua
lua: error loading module 'libsqlite3' from file '/usr/local/lib/lua/5.2/libsqlite3.so':
    /usr/local/lib/lua/5.2/libsqlite3.so: undefined symbol: luaopen_libsqlite3
stack traceback:
    [C]: in ?
    [C]: in function 'require'
    sqlite_test.lua:1: in main chunk
    [C]: in ?
debian@debian:~/Desktop/SQLite Test$ 

这是否意味着 Lua 不能开箱即用"地调用共享库,而我必须使用 Alien 之类的东西?

Does this mean that Lua cannot call shared libs "out the box" and that I HAVE TO use something like Alien?

我知道我还可以对共享库进行编码,使其包含 Lua 头文件,并且我可以制作Lua 函数"...我只是希望我可以调用共享库盒子上的标准".

I know I can also code the shared lib in such a way that it includes the Lua header files and I can make "Lua functions"...I was just hoping I could call shared libs that are "standard" on the box.

如果有帮助或建议,我们将不胜感激;-)

And help or advise would be greatly appreciated ;-)

谢谢

林顿

推荐答案

Lua 不能立即调用 C 库.它不附带 libffi,因此不像 ctypes 那样工作.

Lua cannot call C libraries out of the box. It does not ship with libffi, and as such doesn't work like ctypes.

从历史上看,lua 被嵌入到一个应用程序中,该应用程序反过来会将所需的函数添加到 lua 表中,并提供 lua 堆栈操作来传递和返回参数.

Historically, lua is embedded into an application which in turn will add to the lua tables the needed functions and provide the lua stack manipulation to pass and return parameters.

Alien 是一个 libffi 改编版,可以正常工作.

Alien is a libffi adaptation and will work.

这篇关于Lua 脚本调用 C 共享库的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆