c++调用lua
来源:互联网 发布:淘宝助理 同步宝贝 编辑:程序博客网 时间:2024/04/30 10:43
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
void load(char *filename , int *width , int *height )
{
lua_State *L = lua_open();
luaopen_base(L);
luaL_openlibs(L);
//luaopen_table(L);
//luaopen_io(L);
//luaopen_string(L);
//luaopen_math(L);
if(luaL_loadfile(L ,filename) || lua_pcall(L , 0 , 0 , 0) )
//error(L, "cannot run configuration file: %s",lua_tostring(L, -1));
return ;
lua_getglobal(L,"width");
lua_getglobal(L,"height");
if( !lua_isnumber(L,-2))
//error(L,"Width should be a number\n");
return ;
if(!lua_isnumber(L ,-1))
//error(L,"height should be a number \n");
return ;
*width = (int)lua_tonumber(L,-2);
*height = (int)lua_tonumber(L,-1);
lua_close(L);
}
void main()
{
int a = 1;
int *width = 0 ;
int *height = &a ;
load("test.lua" , width , height);
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
void load(char *filename , int *width , int *height )
{
lua_State *L = lua_open();
luaopen_base(L);
luaL_openlibs(L);
//luaopen_table(L);
//luaopen_io(L);
//luaopen_string(L);
//luaopen_math(L);
if(luaL_loadfile(L ,filename) || lua_pcall(L , 0 , 0 , 0) )
//error(L, "cannot run configuration file: %s",lua_tostring(L, -1));
return ;
lua_getglobal(L,"width");
lua_getglobal(L,"height");
if( !lua_isnumber(L,-2))
//error(L,"Width should be a number\n");
return ;
if(!lua_isnumber(L ,-1))
//error(L,"height should be a number \n");
return ;
*width = (int)lua_tonumber(L,-2);
*height = (int)lua_tonumber(L,-1);
lua_close(L);
}
void main()
{
int a = 1;
int *width = 0 ;
int *height = &a ;
load("test.lua" , width , height);
}
test.lua 配置
width = 100
height = 100
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////实例二
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
lua_State *L;
int luaAdd( int x , int y )
{
int sum;
lua_getglobal( L , "add" );
lua_pushnumber(L , x );
lua_pushnumber(L ,y );
lua_call(L ,2 , 1);
sum = (int)lua_tonumber(L ,-1);
lua_pop(L ,-1);
return sum;
}
int main(int argc , char* argv[] )
{
int sum = 0;
L = lua_open();
luaopen_base(L);
luaL_openlibs(L);
luaL_loadfile(L,"add.lua");
lua_pcall(L,0,LUA_MULTRET , 0);
sum = luaAdd(10,15);
printf("The sum is %d\n" ,sum);
lua_close(L);
return 0;
}
add.lua配置 必须放置与工程目录 .cpp文件同一个文件夹里面
2 function add(x, y)3 return x + y4 end
0 0
- [Lua]C/C++调用Lua
- [lua]C调用lua函数
- C/C++ 调用 Lua
- c调用lua脚本
- Lua调用C函数
- C调用lua
- lua 调用c函数
- lua c/c++ 调用
- Lua调用C函数
- lua调用c函数
- Lua调用C函数
- c调用lua
- c 调用 lua
- c、c++调用lua
- c/c++ 调用lua
- lua调用C函数
- lua调用C函数
- lua调用C函数
- java泛型
- android系列微谈一之——仿微信6.0左右滑动渐变切换效果[大陆第一人_求加精]
- 做网络营销必看干货:月薪3000与月薪30000文案区别
- 在任意两台机器之间push和pull代码
- 第九周项目六 穷举法解决问题之换分币
- c++调用lua
- Fedora12 fetal error:openssl/md5.h: No such file or directory
- 中文时间格式转英文时间格式
- 两个有用的脚本
- Objective C多态
- VS2008中开发手持终端程序(PDA软件)总结
- 数据库视图的使用
- leveldb源码学习 3 - Comparator
- hibernate之关于使用连接表实现多对一关联映射