zoukankan      html  css  js  c++  java
  • 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 .. " ")

    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,

  • 相关阅读:
    HDU5087——Revenge of LIS II(BestCoder Round #16)
    HDU5086——Revenge of Segment Tree(BestCoder Round #16)
    POJ3009——Curling 2.0(DFS)
    POJ2891——Strange Way to Express Integers(模线性方程组)
    算法总结之求解模线性方程组
    Linux运维学习笔记-网络技术知识体系总结
    Linux运维学习笔记-定时任务知识总结
    Linux运维学习笔记-文件权限知识总结
    Linux运维学习笔记-常用快捷键及vi、vim总结
    Linux运维学习笔记-角色知识总结
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5896468.html
Copyright © 2011-2022 走看看