博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lua学习系列(五)
阅读量:6552 次
发布时间:2019-06-24

本文共 2849 字,大约阅读时间需要 9 分钟。

calling C functions from Lua 5.2

这篇文章也不错: 

原文:

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,

 

转载地址:http://gdnco.baihongyu.com/

你可能感兴趣的文章
EL表达式的基本语法
查看>>
Linux solr安装
查看>>
python爬取淘宝商品信息并加入购物车
查看>>
BeanPropertyRowMapper
查看>>
javascript-引用类型的使用
查看>>
新手学习在Ubuntu 14.04搭建Javaweb网站(4)--安装JDK
查看>>
关于Activity的生命周期
查看>>
JAVA中正则表达式总结
查看>>
push的错误
查看>>
HTML <label> 标签的 for 属性
查看>>
【科普】人眼到底等于多少像素
查看>>
推荐 9 个样式化组件的 React UI 库
查看>>
短链接,长链接
查看>>
ubuntu下设置adb环境变量
查看>>
.net 简单循环
查看>>
从HTTP到JDBC完整访问路径日志实现思路
查看>>
DOS常用命令
查看>>
Android 自定义SwitchButtonView实践
查看>>
记录一次linux病毒清除过程
查看>>
linux常用命令目录
查看>>