• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Lua学习系列(五)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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,

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua中调用c++函数发布时间:2022-07-22
下一篇:
使用luagraphql模块让openresty支持graphqlapi发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap