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

查看:368
本文介绍了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.

我听说过"Alien"(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?

例如:在我的情况下,库位于/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无法将共享库称为开箱即用",而我必须使用外星人"之类的东西?

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天全站免登陆