Lua学习系列(五)
calling C functions from Lua 5.2这篇文章也不错: http://blog.csdn.net/x356982611/article/details/26688287 http://www.troubleshooters.com/codecorn/lua/lua_lua_calls_c.htm 原文:http://lua-users.org/lists/lua-l/2012-04/msg01008.html I’m in the process of developing an automated test harness for a product we are developing. The test harness will use a combination of Lua and C. One of the goals is to “hide” all the details so that when the software developers want to test their code modules, they can write script files which are used as input to the test harness.
I’ve been having some issues calling C functions from Lua 5.2
Here’s a portion of the C file (and ignore the fact that it is just returning dummy values):
#include <stdio.h>
/* include Lua libraries */
#include "lua.h" #include "lauxlib.h" #include "lualib.h"
static int create_message(lua_State *L) { int dest; int payload; int numops; int msg_ID;
/* get number of arguments */
numops = lua_gettop(L);
/* get destination ID */
dest = lua_tonumber(L,1);
/* get payload */
payload = lua_tonumber(L,2);
/* call function to generate message ID */
msg_ID = payload * 10;
/* push message ID onto stack */
lua_pushnumber(L,msg_ID);
/* return the number of results */
return 1; }
/* table of functions accessible from Lua */
static const struct luaL_Reg testHarness [] = { {"buildMessage", create_message}, {NULL,NULL} };
int luaopen_testHarness(lua_State *L) { luaL_newlib (L, testHarness); /* register C functions with Lua */ return 1; }
I compiled the C file and created a Linux dynamic library named “testHarness.so”. Here’s a simple Lua file (named “lua_test.lua”) that tests the Lua to C interface:
#!/usr/local/bin/lua
-- Test harness script file
require("testHarness") -- link in testHarness C library
-- call C routine
message_ID = buildMessage(10, 20) print("Returned message_ID = " .. message_ID .. "\n")
When I run run lua_test.lua from the Linux command line, I get an error message that says “attempt to call global ‘buildMessage’ (a nil value)”.
If I rewrite the require statement as:
th = require(“testHarness”)
and change the function call to:
message_ID = th.buildMessage(10,20)
everything then works fine. No error occurs and the print statement is executed.
Is there something I can do so that I don’t need to have the “th.” In front of the call to the buildMessage function in the C library? Eventually, I plan to take the function call out of here and pass in an input file name as a command line parameter. The input file will be the script file written by the software developers and it will simply contain a series of buildMessage() calls and calls to other soon-to-be-developed functions.
For simplicity, I would prefer that the developers not have to put “th.” In front of every function call. I’ve Googled for answers but haven’t really found any. One webpage I found had an example that seemed to do what I wanted, but it was written in Lua 5.1 and used some deprecated functions. Any suggestions to solve this issue would be appreciated.
Thanks,
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现