C++ rtmp livestream 流媒体

海阔天空 张翼飞翔

我的学习笔记。--我喜欢这里,因为这里安静,无广告骚扰。
随笔 - 82, 文章 - 2, 评论 - 126, 引用 - 0
数据加载中……

C++中使用Lua脚本 和lua中调用c的方法

参考http://blog.csdn.net/kun1234567/archive/2007/12/11/1929815.aspx
第1步:下载
从官方主页www.lua.org下载Lua源代码,最新版本为5.1.3。
解压之后找到“[Lua]/src”文件夹,这里面就是Lua了,不过还不能直接使用。

第2步:编译lua
使用任意ANSI C编译器,在这里使用VS2005编译LUA。具体步骤如下:
a.打开vs的命令行工具,工具-->visual studio 2005 command prompt
b.跳转到[Lua]目录,例如:cd  D:/Program Files/Lua
c.执行:etc\luavs.bat( 注意,是 \ 不是 /,写错了不能执行编译 )
d.然后lua51.dll, lua51.lib, lua.exe, and luac.exe就生成在 src 路径下了
e.在windows环境变量中把[Lua]\src添加到系统Path中去,

第3步:创建lua脚本
下载LuaEdit http://luaforge.net/frs/download.php/1037/LuaEdit_2_5.zip 他是Lua脚本的编辑工具,还可以对Lua脚本进行语法检测和调试。你也可以其他的纯文本编辑工具写一个文件test.lua,注意后面是没有分号‘;’的
function f ( x, y)
    
return x + y
end

第4步:在C++ 中调用Lua脚本
开启VC++6.0环境创建一个新文件main.cpp
// Win32Console.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"

extern "C"
{
#include 
"D:/Lib/Lua/lua.h"
#include 
"D:/Lib/Lua/lualib.h"
#include 
"D:/Lib/Lua/lauxlib.h"
}

#pragma comment( lib ,
"D:/Lib/Lua/lua51.lib")
lua_State 
*L;

//调用lua
double fun( double x, double y )
{
 
double ret;
 lua_getglobal( L, 
"add");        // 获取全局变量f
 lua_pushnumber( L,x);          // 操作数压栈
 lua_pushnumber( L,y);          // 操作数压栈
 lua_call( L, 21);            // 执行:2个操作数,1个返回值
 
//lua_pcall( L, 2, 1, 0);      // 保护模式的lua_call,0为错误处理码。具体应用暂时不明,在使用手册中有粗略介绍
 ret = lua_tonumber( L, -1);    // 将栈顶元素转换成数字并赋值给ret
 lua_pop( L, 1);                // 从栈中弹出一个元素
 return ret;
}


//被lua调用的方法
static int average(lua_State *L2)
{
    
/* get number of arguments */
    
int n = lua_gettop(L2);
    
double sum = 0;
    
int i;
    
/* loop through each argument */
    
for (i = 1; i <= n; i++)
    
{
    
/* total the arguments */
    sum 
+= lua_tonumber(L2, i);
    }

    lua_pushnumber(L, sum 
/ n);
    
/* push the sum */
    lua_pushnumber(L, sum);
    
/* return the number of results */
    printf(
"average called. [ok]\n");
    
return 2;
}

//==============================================
// Main Functions
//==============================================
int main( void)
{
 
int error;
 L 
= lua_open();          // 创建Lua接口指针(借用DX的术语,本质是个堆栈指针)
 luaopen_base(L);     // 加载Lua基本库
 luaL_openlibs(L);     // 加载Lua通用扩展库
/*
 可能有的文章会采用以下写法,手工控制加载哪些库:
 luaopen_table(L);     // 加载table库
 luaopen_io(L);           // 加载IO库
 luaopen_string(L);    // 加载string库
 luaopen_math(L);     // 加载math库
 经过测试,luaopen_io(L);该句执行异常,可能跟Lua的IO库有关系。具体原因暂时没有追究,将来如果有机会弄清楚,再回头来阐述。
*/

 
 
/* load the script */
 lua_register(L, 
"average", average);
 error 
= luaL_dofile(L, "hellow.lua");    // 读取Lua源文件到内存编译

 
double ret = fun( 103.4);                // 调用模版函数f
 printf( "ret = %f\n", ret);                // 输出结果,C语言的东西,跟Lua无关
 lua_close( L);
 
return 1;
}


创建一个hellow.lua文件和main.cpp放在一起,写入以下内容
function add ( x, y)    
    file 
= assert(io.open("data.txt""w"))
    file:write(
"abcde\n")
    file:write(
"ok!\n")
    file:close()

    
--DataDumper(1,2,3,4)
    file 
= assert(io.open("data.txt""r"))
    str 
= file:read("*a")
    io.write(str)
    io.write(
"\n")
    
    avg, sum 
= average(102003000)
    print(
"The average is ", avg)
    print(
"The sum is ", sum)
    
    
return x + y
end

posted on 2008-04-21 17:11 ZhangEF 阅读(6098) 评论(4)  编辑  收藏 所属分类: C/C++

评论

# re: C++中使用Lua脚本 和lua中调用c的方法  回复  更多评论   

获益良多
2010-02-09 13:27 | alsky

# re: C++中使用Lua脚本 和lua中调用c的方法  回复  更多评论   

Thanks 受益匪浅啊,要是能把hellow.lua文件里面的再解释一下就更完美了。
2010-06-17 23:07 | Ronaldo

# re: C++中使用Lua脚本 和lua中调用c的方法  回复  更多评论   

忠心的感谢, 加我好友吧,加我时请注明LUA
2011-05-17 11:35 | 鸿鹄

# re: C++中使用Lua脚本 和lua中调用c的方法  回复  更多评论   

我的QQ88046741
2011-05-17 11:35 | 鸿鹄