lua调用c++
2017-08-01 19:20
162 查看
TestLib.h
//
// TestLib.h
// TestLua
//
// Created by jianan on 2017/8/1.
//
//
#ifndef TestLib_h
#define TestLib_h
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
// 全局的函数,用于导出,内部完成注册和初始化功能
int luaopen_testLib(lua_State *L);
#endif /* TestLib_h */TestLib.cpp
//
// TestLib.cpp
// TestLua
//
// Created by jianan on 2017/8/1.
//
//
#include <stdio.h>
#include "TestLib.h"
static int testFunc(lua_State *L)
{
printf("http://www.jellthink.com\n");
lua_pushnumber(L, 250);
lua_pushstring(L, "hello lua");
return 2; //表示2个参数
}
static const struct luaL_Reg myLib[] =
{
{"test", testFunc},
{NULL, NULL}
};
int luaopen_testLib(lua_State *L)
{
luaL_register(L, "testLib", myLib);
return 1; // 把表压入了栈中,所以就需要返回1
}
AppDelegate.cpp
#include "TestLib.h"
config.lua
require "testLib"
local a, b = testLib.test()
print(a..b)
--http://www.jellthink.com
--250hello lua
//
// TestLib.h
// TestLua
//
// Created by jianan on 2017/8/1.
//
//
#ifndef TestLib_h
#define TestLib_h
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
// 全局的函数,用于导出,内部完成注册和初始化功能
int luaopen_testLib(lua_State *L);
#endif /* TestLib_h */TestLib.cpp
//
// TestLib.cpp
// TestLua
//
// Created by jianan on 2017/8/1.
//
//
#include <stdio.h>
#include "TestLib.h"
static int testFunc(lua_State *L)
{
printf("http://www.jellthink.com\n");
lua_pushnumber(L, 250);
lua_pushstring(L, "hello lua");
return 2; //表示2个参数
}
static const struct luaL_Reg myLib[] =
{
{"test", testFunc},
{NULL, NULL}
};
int luaopen_testLib(lua_State *L)
{
luaL_register(L, "testLib", myLib);
return 1; // 把表压入了栈中,所以就需要返回1
}
AppDelegate.cpp
#include "TestLib.h"
bool AppDelegate::applicationDidFinishLaunching() { lua_State* L = engine->getLuaStack()->getLuaState(); lua_module_register(L); luaopen_testLib(L); }
config.lua
require "testLib"
local a, b = testLib.test()
print(a..b)
--http://www.jellthink.com
--250hello lua
相关文章推荐
- Lua学习之1 :C++调用Lua函数
- C++调用Lua的函数
- (如何在C++代码中调用Lua脚本的函数?)
- lua和c/c++互相调用实例分析
- C++调用LUA
- Quick-Cocos2d-x 使用tolua工具导出C++的类给Lua调用
- c++中让lua绑定类,使lua直接调用c++中定义的类
- c++调用lua
- lua与c++的相互调用入门篇
- cocos2d-x 3.0 在C++中调用lua函数
- C++ 调用Lua简单例子
- Quick-Cocos2dx-Community lua绑定,lua调用C++ 类
- lua调用c++
- c++调用lua的基本操作
- Lua 与C/C++ 交互系列: Lua调用C/C++函数(3)
- 关于Lua调用c++ DLL 文件的详解
- Lua 与C/C++ 交互系列: Lua调用C/C++函数(4-2)
- c/c++与lua相互调用
- 9.Lua调用c++函数获取返回值
- 将C++对象导出到lua,C++和lua的相互调用