软件
swigwin3    用于生成c的lua包装
lua5.2源代码

步骤
进入目录G:\sw\swigwin-3.0.12\Examples\lua\arrays
执行 SWIG -lua     example.i, 就会生成文件example_wrap.c
进入lua source code G:\doc\lua\lua-5.2.4.tar\lua-5.2.4\src
拷贝swig生成的几个c文件到该目录

b编写一个新的mymain.c内容如下

执行cl编译生成example.exe

执行example.exe runme.lua

代码:

===example.i

/* File : example.i */
%module example

/* in this file there are two sorting functions
and three different ways to wrap them.

See the lua code for how they are called
*/

%include <carrays.i>    // array helpers

// this declares a batch of function for manipulating C integer arrays
%array_functions(int,int)

// this adds some lua code directly into the module
// warning: you need the example. prefix if you want it added into the module
// admittedly this code is a bit tedious, but its a one off effort
%luacode {
function example.sort_int2(t)
-- local len=table.maxn(t) -- the len - maxn deprecated in 5.3
 local len=0; for _ in pairs(t) do len=len+1 end
 local arr=example.new_int(len)
 for i=1,len do
  example.int_setitem(arr,i-1,t[i]) -- note: C index is one less then lua index
 end
 example.sort_int(arr,len) -- call the fn
 -- copy back
 for i=1,len do
  t[i]=example.int_getitem(arr,i-1) -- note: C index is one less then lua index
 end
 example.delete_int(arr) -- must delete it
end
}

// this way uses the SWIG-Lua typemaps to do the conversion for us
// the %apply command states to apply this wherever the argument signature matches
%include <typemaps.i>
%apply (double *INOUT,int) {(double* arr,int len)};

%inline %{
extern void sort_int(int* arr, int len);
extern void sort_double(double* arr, int len);
%}

===runme.lua

---- importing ----
if string.sub(_VERSION,1,7)=='Lua 5.0' then
    -- lua5.0 doesnt have a nice way to do this
    lib=loadlib('example.dll','luaopen_example') or loadlib('example.so','luaopen_example')
    assert(lib)()
else
    -- lua 5.1 does
    require('example')
end

-- a helper to print a Lua table
function print_table(t)
    print(table.concat(t,","))
end

-- a helper to print a C array
function print_array(arr,len)
    for i=0,len-1 do
        io.write(example.int_getitem(arr,i),",")
    end
    io.write("\n")
end

math.randomseed(0)  -- init random

--[[ version 1: passing a C array to the code
let's test call sort_int()
this requires a C array, so is the hardest to use]]
ARRAY_SIZE=10
arr=example.new_int(ARRAY_SIZE)
for i=0,ARRAY_SIZE-1 do
    example.int_setitem(arr,i,math.random(1000))
end
print "unsorted"
print_array(arr,ARRAY_SIZE)
example.sort_int(arr,ARRAY_SIZE)
print "sorted"
print_array(arr,ARRAY_SIZE)
example.delete_int(arr) -- must delete it
print ""

--[[ version 2: using %luacode to write a helper
a simpler way is to use a %luacode
which is a lua function added into the module
this can do the conversion for us
so we can just add a lua table directly
(what we do is move the lua code into the module instead)
]]
t={}
for i=1,ARRAY_SIZE do
    t[i]=math.random(1000)
end
print "unsorted"
print_table(t)
example.sort_int2(t) -- calls lua helper which then calls C
print "sorted"
print_table(t)
print ""

--[[ version 3: use a typemap
this is the best way
it uses the SWIG-Lua typemaps to do the work
one item of note: the typemap creates a copy, rather than edit-in-place]]
t={}
for i=1,ARRAY_SIZE do
    t[i]=math.random(1000)/10
end
print "unsorted"
print_table(t)
t=example.sort_double(t) -- replace t with the result
print "sorted"
print_table(t)

===mymain.c

#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

extern int luaopen_example(lua_State* L); // declare the wrapped module

int main(int argc, char* argv[])
{
 lua_State *L;
 if (argc<2)
 {
  printf("%s: <filename.lua>\n", argv[0]);
  return 0;
 }
 L= luaL_newstate();
 luaL_openlibs(L);
 //luaopen_base(L);       // load basic libs (eg. print)
 //luaopen_math(L);
 luaopen_example(L);    // load the wrapped module
 if (luaL_loadfile(L, argv[1])==0) // load and run the file
  lua_pcall(L, 0, 0, 0);
 else
  printf("unable to load %s\n", argv[1]);
 lua_close(L);
 return 0;
}

结果:

参考:

file:///G:/sw/swigwin-3.0.12/Doc/Manual/Lua.html#Lua_nn2

Use swig + lua quick guide的更多相关文章

  1. Nemerle Quick Guide

    This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...

  2. 29 A Quick Guide to Go's Assembler 快速指南汇编程序:使用go语言的汇编器简介

    A Quick Guide to Go's Assembler 快速指南汇编程序:使用go语言的汇编器简介 A Quick Guide to Go's Assembler Constants Symb ...

  3. (转) Quick Guide to Build a Recommendation Engine in Python

    本文转自:http://www.analyticsvidhya.com/blog/2016/06/quick-guide-build-recommendation-engine-python/ Int ...

  4. Quick guide for converting from JAGS or BUGS to NIMBLE

    Converting to NIMBLE from JAGS, OpenBUGS or WinBUGS NIMBLE is a hierarchical modeling package that u ...

  5. Quick Guide to Microservices with Spring Boot 2.0, Eureka and Spring Cloud

    https://piotrminkowski.wordpress.com/2018/04/26/quick-guide-to-microservices-with-spring-boot-2-0-eu ...

  6. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  7. [比较老的文章]三维渲染引擎 OGRE 与 OSG 的比较综述

    1 .引言随着计算机可视化.虚拟现实技术的飞速发展,人们对实时真实感渲染以及场景复杂度提出了更高的要求.传统的直接使用底层图形接口如OpenGL.DirectX开发图形应用的模式越来越暴露出开发复杂性 ...

  8. Linux 新手非常有用的命令

    http://www.cnblogs.com/felix-/p/4341773.html Linux 新手非常有用的命令 你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟! ...

  9. 转:关于 OGRE 与 OSG 的简单比较

    1   前言 我曾经细致阅读过 OGRE 和 OSG 官方提供的文档,有<Pro OGRE 3D Programming>.OGRE自带手册(manual).王锐老师等翻译的<Ope ...

随机推荐

  1. 转--让一个运行在SYSTEM权限下的进程与当前用户的桌面进行交互

    #define DESKTOP_ALL ( DESKTOP_READOBJECTS | DESKTOP_CreateWINDOW | \ DESKTOP_CreateMENU | DESKTOP_HO ...

  2. Linux:磁盘配额

    磁盘配额 一.简略步骤显示 第一步:关闭虚拟机 第二步:编辑虚拟机设置--硬盘--添加 第三步:查看磁盘分区情况 fdisk -l 第四步:选择磁盘分区 fdisk /dev/sda2 第五步:选择磁 ...

  3. Arduino+Avr libc制作Badusb原理及示例讲解

    一. 前言 2014年美国黑帽大会上研究人员JakobLell和Karsten Nohl展示了badusb的攻击方法后,国内与badusb相关的文章虽然有了一些,但是大部分人把相关文章都阅读后还是会有 ...

  4. 关于lunece的搜索的分页和多字段搜索关键词

    关于全文检索lunece的分页,我们需要用到的是以下方法 IndexSearch类下的searchAfter方法. IndexSearch isearch=new IndexSearch(a); is ...

  5. [Jedis] ERR wrong number of arguments for 'mget'

    看别人写的代码是件比较痛苦的事情,更加痛苦的是别人的代码出错还要负责调试好. 关于如何迅速定位问题和调试代码,我的一点感受是:逐行认真查看错误信息,在这些信息中找自己熟悉的内容(包括文件名.方法名等) ...

  6. winform 与百度搜索智能提示

    private void textBox1_TextChanged(object sender, EventArgs e) { listBox1.Items.Clear(); if (string.I ...

  7. QueryString中的加号变成空格解决方法

    通过Request.QueryString["CheckItem"]的方式调用值的时候,数值中的加号“+”会转换为空格“ ” 例如传输“ABC+EFG”,就会取到“ABC EFG” ...

  8. PhoneGap Vs AppCan

    首先在写这篇文章前,必须先申明一下,本人是技术出身,对HTML技术及手机客户端都有过编程经验,只是出于工作岗位的变动,便没有再具体代码工作,以下文章涉及的中间件的基本代码实现及前期的API使用,都是自 ...

  9. 【Java】输出目录结构

    import java.io.*; import java.io.File; import java.io.IOException; public class FileUtil { public st ...

  10. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...