简体   繁体   English

在C中嵌入lua代码

[英]embedding lua code in c

I am attempting to follow the besic guide given here on embedding lua into C. I copied the code verbatim into my own embed.c file and executed the exact compiler command listed: 我试图遵循此处给出的besic指南,将lua嵌入到C中。我将代码逐字复制到了自己的embed.c文件中,并执行了列出的确切编译器命令:

cc -o embed embed.c \
            -I/usr/local/include \
            -L/usr/local/lib \
            -llua -llualib

I get the error: 我收到错误:

embed.c:19:14: error: invalid storage class for function ‘openlualibs’

After which I moved the functions outside of main , compiled again, and got: 之后,将函数移出main ,再次进行编译,并得到:

/usr/bin/ld: cannot find -llualib

I am at a loss for why I cannot compile this. 我不知道为什么不能编译它。 lua is installed properly. lua已正确安装。 has anyone else encountered these problems? 还有其他人遇到过这些问题吗? If this is a bad tutorial, please feel free to simply direct me to a batter one. 如果这是一个糟糕的教程,请随意将我引导到一个连击者。

On some Linux distributions you may need to install the lua-devel (or similar named) package, in order to get the proper header files and library symlinks required for compiling and linking projects against the package. 在某些Linux发行版上,您可能需要安装lua-devel (或类似名称的)软件包,以获取正确的头文件和库符号链接,以根据该软件包编译和链接项目。 If you do have a liblualib-<version>.so.<version> , for example liblualib-5.so.5.0 , you may need to install the devel package. 如果确实具有liblualib-<version>.so.<version> ,例如liblualib-5.so.5.0 ,则可能需要安装devel软件包。

Starting with lua 5.1, liblualib does not exist. 从lua 5.1开始, liblualib不存在。 Here is the release announcement: http://lua-users.org/lists/lua-l/2005-05/msg00186.html 这是发行公告: http : //lua-users.org/lists/lua-l/2005-05/msg00186.html

I've had somewhat similar problems when embedding Lua. 嵌入Lua时遇到了类似的问题。 What I found that works for me is linking the dynamic link library (dl) and the math library (m). 我发现对我有用的是链接动态链接库(dl)和数学库(m)。 The math library may not be necessary if you're not using the lmath standard library. 如果您不使用lmath标准库,则可能不需要数学库。

cc -o embed embed.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl

This, of course, assumes that /usr/local/ is where the Lua files are installed, which is probably true. 当然,这假定/ usr / local /是Lua文件的安装位置,这可能是正确的。

As for the tutorial you linked to, I think it may be very out of date. 至于您链接的教程,我认为可能已经过时了。 Besides liblualib no longer existing, there are individual functions to open each standard library. 除了不再存在的liblualib外,还有一些单独的函数可以打开每个标准库。 These are the luaopen_* functions. 这些是luaopen_ *函数。 Here's the relevant 5.1 reference manual entry. 这是相关的5.1参考手册条目。 (I assume you're using 5.1, since that seems to be the version available in the packages) As for a better tutorial, I suggest the Programming in Lua book. (我假设您使用的是5.1,因为它似乎是软件包中可用的版本。)至于更好的教程,我建议使用《 Lua编程》一书。 Unfortunately, it was written for Lua 5.0. 不幸的是,它是为Lua 5.0编写的。 It is still mostly relevant, but I suggest you look over the relevant sections of the 5.1 reference manual, too. 它仍然是最相关的,但是我建议您也仔细阅读5.1参考手册的相关部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM