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,

Lua学习系列(五)的更多相关文章

  1. scrapy爬虫学习系列五:图片的抓取和下载

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  2. Lua学习系列(二)

    资源整理: 风云老师博客: http://blog.codingnow.com/eo/luaoeeeaeau/ 知乎: https://www.zhihu.com/question/20736660 ...

  3. Lua学习系列(一)

    从现在开始,打算学习一门新的脚本语言-lua. 1.什么是lua? a) lua1 • Lua 1.0 was implemented as a library, in less then 6000 ...

  4. Java NIO学习系列五:I/O模型

    前面总结了很多IO.NIO相关的基础知识点,还总结了IO和NIO之间的区别及各自适用场景,本文会从另一个视角来学习一下IO,即IO模型.什么是IO模型?对于不同人.在不同场景下给出的答案是不同的,所以 ...

  5. [jQuery学习系列五 ]5-Jquery学习五-表单验证

    前言最近总是有一个感觉,虽然这些东西都自己学习并一个案例一个案例的去验证过了.但是总觉得不写成博客记录下来这些都不是自己的东西(心理作用,哈哈).所以每当学习或者复习相关的知识我都喜欢记录下来,下面开 ...

  6. MVC3+EF4.1学习系列(五)----- EF查找导航属性的几种方式

    文章索引和简介 通过上一篇的学习 我们把demo的各种关系终于搭建里起来 以及处理好了如何映射到数据库等问题 但是 只是搭建好了关系 问题还远没有解决 这篇就来写如何查找导航属性 和查找导航属性的几种 ...

  7. STL学习系列五:Queue容器

    Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...

  8. Lua学习系列(四)

    lua 资源:http://www.dcc.ufrj.br/~fabiom/lua/ 第一个Lua程序 http://www.dcc.ufrj.br/~fabiom/lua/ 原文:https://w ...

  9. RabbitMQ入门学习系列(五) Exchange的Direct类型

    快速阅读 利用Exchange的Direct类型,实现对队列的过滤,消费者启动以后,输入相应的key值,攻取该key值对应的在队列中的消息 . 从一节知道Exchange有四种类型 Direct,To ...

随机推荐

  1. ios开发使用lipo命令合并真机库和模拟器库

    在开发ios时,我们经常会遇到编译两套库文件,使用模拟器时链接模拟器库,使用真机时使用真机库,这样操作会对后期的维护带来麻烦,所以Apple提供了一个把多个不同平台的.a库文件合并成一个适用于多平台的 ...

  2. Goods transportation

    Goods transportation time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. VS2013使用技巧汇总

    1. Peek View 在不新建TAB的情况下快速查看.编辑一个函数的代码. 以前要看一个函数的实现,需要在使用的地方点击F12跳转到该函数,实际上这是很浪费时间的.VS2013Peek View便 ...

  4. 美团,点评,澎湃等APP的启示

    事先声明,因个人能力尚浅,文章若有不足之处,望留言指出,也欢迎成为好朋友. 本来想打算写团购类APP的竞品分析,但是发现不管是天天果园这样生鲜APP,还是澎湃这样的新闻资讯APP,思路差不多,都是: ...

  5. CI 路由设置

    $route['news/show/(:num)']='news/show/$1';   前边是匹配 网址   后边是  指定控制器 在application/config/文件夹下有routes.p ...

  6. 快学Scala-第一章 基础

    知识点: Scala程序并不是一个解释器,实际发生的是,你输入的内容被快速的编译成字节码,然后这段字节码交由Java虚拟机执行. 以val定义的值是一个常量,以var定义的值是一个变量,声明值或变量但 ...

  7. [转]MD5加密算法的java实现

    import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /* * MD5 算法 */ pu ...

  8. java 对象的组合,一个类组合到另一个类中(例如手机卡装到手机上)

    Example4_9.java public class Example4_9 { public static void main(String args[]) { SIM simOne = new ...

  9. eclipse,android Localization (Internationalization) 安卓本地化(国际化)

    1.创建新的资源文件,名字保持一致.提示"已存在",继续. 2.使用“语言”作为识别器,然后选择相应的语言代码.Tips:其他的适配,如国家.屏幕大小等,也是通过这里的识别器实现适 ...

  10. java 工作内存

    所谓线程的“工作内存”到底是个什么东西?有的人认为是线程的栈,其实这种理解是不正确的.看看JLS(java语言规范)对线程工作 内存的描述,线程的working memory只是cpu的寄存器和高速缓 ...