Lua学习笔记6:C++和Lua的相互调用
//main.cpp
#include <iostream> extern "C"
{
#include "lualib/lua.h"
#include "lualib/lualib.h"
#include "lualib/lauxlib.h"
} using namespace std; int main()
{
lua_State* L = luaL_newstate() ;
luaopen_base(L) ;// 不知道为什么这里使用luaL_openlibs(L)会报错
luaL_dofile(L, "script/test.lua") ;
lua_getglobal(L, "add") ;
lua_pushnumber(L, 1) ;
lua_pushnumber(L, 2) ;
lua_pcall(L, 2, 1, 0) ;
int sum = (int)lua_tonumber(L, -1) ;
lua_pop(L, -1) ;
lua_close(L) ;
cout << "1 + 2 = " << sum << endl;
return 0 ;
}
function add(x, y)
return x + y
end
编译命令:
g++ main.cpp -L./lualib -llua
实际上就C++通过Lua脚本调用自己的函数。
cocos2dx中那些个Lua接口,调用的都是C++中定义的函数。
// main.cpp
#include <iostream> extern "C"
{
#include "lualib/lua.h"
#include "lualib/lualib.h"
#include "lualib/lauxlib.h"
} using namespace std; lua_State* L ; static int average(lua_State *L)
{
// num of args
int n = lua_gettop(L) ;
double sum = 0 ;
int i = 0 ;
for ( i = 1; i <= n; ++i)
{
sum += lua_tonumber(L, i) ;
} // push the average
lua_pushnumber(L, sum / n) ;
// push the sum
lua_pushnumber(L, sum) ; return 2;
} int main()
{
L = luaL_newstate() ;
luaopen_base(L) ;
// register function
lua_register(L, "average", average) ;
luaL_dofile(L, "test.lua") ;
lua_close(L) ;
return 0 ;
}
avg, sum = average(1, 2, 3, 4, 5)
print ("The average is ", avg)
print ("The sum is ", sum)
先就到这里了。假设有错误还请路过的大神指点一二。
Lua学习笔记6:C++和Lua的相互调用的更多相关文章
- [转]LUA 学习笔记
Lua 学习笔记 入门级 一.环境配置 方式一: 1.资源下载http://www.lua.org/download.html 2.用src中的源码创建了一个工程,注释调luac.c中main函数,生 ...
- Lua学习笔记(二):基本语法
Lua学习指南:http://www.lua.org/manual/ 首先我们要明确的一点是:在Lua中,除了关键字外一切都是变量. Lua关键字 可以查看这个地址:http://www.lua.or ...
- Lua 学习笔记(一)
Lua学习笔记 1.lua的优势 a.可扩张性 b.简单 c.高效率 d.和平台无关 2.注释 a.单行注释 -- b.多行注释 --[[ --]] 3.类型和 ...
- Lua学习笔记4. coroutine协同程序和文件I/O、错误处理
Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...
- (转)Lua学习笔记1:Windows7下使用VS2015搭建Lua开发环境
Lua学习笔记1:Windows7下使用VS2015搭建Lua开发环境(一)注意:工程必须添加两个宏:“配置属性”/“C或C++”/“预处理器”/“预处理器定义”,添加两个宏:_CRT_SECURE_ ...
- Lua学习笔记:面向对象
Lua学习笔记:面向对象 https://blog.csdn.net/liutianshx2012/article/details/41921077 Lua 中只存在表(Table)这么唯一一种数据结 ...
- 《深入Java虚拟机学习笔记》- 第19章 方法的调用与返回
<深入Java虚拟机学习笔记>- 第19章 方法的调用与返回
- go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用)
目录 go微服务框架kratos学习笔记四(kratos warden-quickstart warden-direct方式client调用) warden direct demo-server gr ...
- Java web与web gis学习笔记(二)——百度地图API调用
系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...
随机推荐
- DOM Traversal Example | Documentation | Qt Project
DOM Traversal Example | Documentation | Qt Project DOM Traversal Example
- software quality assurance 常见问题收录
1. What is Quality? Quality means, “meeting requirements.” ..Whether or not the product or service d ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- mysql支持emoji解决办法
mysql显示不了emoji表情或者显示??,原因这里不解释,直接说解决办法.(主要就是修改utf8mb4) 1.修改表 ALTER TABLE `TABLE_NAME` CHARACTER SET ...
- 【随手记-有空整理】Linux随手记
1. CentOS6.5安装g++:yum install gcc-c++ 注意如果写成yum install g++会提示No package g++ available. 2. XShell下打开 ...
- java-输出格式
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html Formatting Numeric Print Output ...
- [翻译]Django速查表
原文在此:https://code.djangoproject.com/wiki/DjangoCheatSheet Django速查表Django教程已经非常好了.这个速查表的作用是创建一个快速开始指 ...
- 使用python操作RabbitMQ,Redis,Memcache,SQLAlchemy 其一
一.概念 1.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态 ...
- 分支-15. 日K蜡烛图(15)
#include<iostream> using namespace std; int main(){ float o,h,l,c; while(cin>>o>>h ...