Lua调用C,C++函数案例
该程序主要是C++与Lua之间的相互调用示例。
执行内容:
(1)新建一个lua_State
(2)打开常用库,如io,os,table,string等
(3)注册C函数
(4)导入程序所在目录下所有*.lua文件
(5)调用Lua中的MainEntry()函数
我们可能在lua_functions.cpp文件中加入我们新增的C函数,将C++与Lua结合为一个更强大的程序。
下载到本地之后,将Makefile.txt改成Makefile,然后make就可以了。
存在的问题:好像在main()调用MainEntry()时,传入的argv在MainEntry(...)中的arg中没有。
1. [文件] main.cpp ~
#include <iostream>
#include <vector>
#include <lua.hpp>
#include "type.h"
#include "util.h"
using namespace std;
extern lua_register_t lua_cfunction_list[];
void open_all_libs(lua_State *L)
{
luaopen_base(L);
luaopen_io(L);
luaopen_os(L);
luaopen_string(L);
luaopen_table(L);
luaopen_math(L);
luaopen_bit32(L);
}
void register_functions(lua_State *L)
{
lua_register_t *p = lua_cfunction_list;
while (p->name) {
lua_pushcfunction(L, p->pfunc);
lua_setglobal(L, p->name);
++p;
}
}
bool load_lua_files(lua_State *L, const char *dir_path)
{
vector<string> file_list;
find_files("./", ".lua", file_list);
vector<string>::iterator iter = file_list.begin();
for (; iter != file_list.end(); ++iter) {
if (luaL_dofile(L, iter->c_str())) {
cout << "ERR: loadfile " << *iter << " fail: " << lua_tostring(L, -1) << endl;
return false;
}
}
return true;
}
int main (int argc, char **argv)
{
lua_State *L = luaL_newstate();
if (L == NULL) {
cout << "ERR: new state fail!" << endl;
return 0;
}
/// open all lua library, such as: io,os,string,table,math...
open_all_libs(L);
/// register C functions to lua
register_functions(L);
/// load lua file in ./ directory.
load_lua_files(L, "./");
/// run lua function "MainEntry()"
lua_getglobal(L, "MainEntry");
if (lua_type(L, -1) == LUA_TFUNCTION) {
for (int i = 0; i < argc; ++i) {
lua_pushstring(L, argv[i]);
}
lua_pcall(L, argc, 0, 0);
} else {
cout << "ERR: can't find function \"MainEntry\"" << endl;
}
return 0;
}
2. [文件] lua_functions.cpp ~
#include <iostream>
#include <lua.hpp>
#include "type.h"
using namespace std;
/**
* define all C lua functions below
*/
int l_hello(lua_State *L)
{
cout << "Hello! This is C function." << endl;
}
/// add function and name in below table.
lua_register_t lua_cfunction_list [] = {
"hello", l_hello,
NULL
};
3. [文件] type.h ~
#ifndef __LUA_TYPE_H__
#define __LUA_TYPE_H__
#include <lua.hpp>
struct lua_register_t {
const char *name;
lua_CFunction pfunc;
};
#endif //__LUA_TYPE_H__
4. [文件] util.h ~
#ifndef __UTIL_H__
#define __UTIL_H__
#include <vector>
#include <string>
using namespace std;
extern void find_files(const string &dir_path, const string &part_name, vector<string> &file_list);
#endif //__UTIL_H__
5. [文件] util.cpp ~
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "util.h"
void find_files(const string &dir_path, const string &part_name, vector<string> &file_list)
{http://www.enterdesk.com/special/shouhui/
DIR *dir = opendir(dir_path.c_str());
if (dir) {手绘图片
struct stat statbuf;
struct dirent *entry = NULL;
while ((entry = readdir(dir)) != NULL) {
string entry_name(entry->d_name);
lstat(entry->d_name, &statbuf);
if (S_ISDIR(statbuf.st_mode)) {
if (entry_name == "." || entry_name == "..")
continue;
find_files(dir_path + entry_name + '/', part_name, file_list);
}
size_t pos = entry_name.find(part_name);
if (pos != std::string::npos) {
string file_full_name = dir_path + entry_name;
file_list.push_back(file_full_name);
}
}
closedir(dir);
}
}
6. [文件] Makefile.txt ~
Target = run_lua
Lib = lua_cfunc.a
CC = g++
AR = ar
all : lua_functions.o lib
$(CC) -o $(Target) -lm lua_functions.o $(Lib) /usr/local/lib/liblua.a
lib : main.o util.o
$(AR) crv $(Lib) main.o util.o
%.cpp: %o
$(CC) -c $<
main.o : main.cpp type.h util.h
util.o : util.cpp util.h
lua_functions.o : lua_functions.cpp type.h
clean:
rm *.o
7. [文件] main.lua ~ 181B
print("loading files ...");
function MainEntry(...)
print("This is MainEntry()")
hello()
for i, v in ipairs(arg) do
print(i .. "=" .. tostring(v))
end
end
8.代码
[README]
main.cpp --主函数文件,在main()中包含了导入库、注册C函数、导入LUA文件操作,最后调用MainEntray()函数。
util.h util.cpp --定义find_files()函数,该函数功能是查找指定目录下的文件名,返回vector<string>
type.h --定义lua_cfunction_table函数注册列表
lua_functions.cpp --定义C函数,添加新的C函数库则加在这个文件里,不需要改动其它。
run_lua --编译生成的可执行文件
--执行该文件,注册lua_functions.cpp中所有的C函数,递归加载当前目录下所有*.lua文件,调用MainEntry()
Lua调用C,C++函数案例的更多相关文章
- lua调用的c函数模块命名规则
比如lua中调用C函数 socket.core() , 在c中需要这样命名:luaopen_socket_core(); 基本模式是添加luaopen_前缀,讲.变为_
- Lua 调用的 C 函数保存 state 的两种方式: Storing State in C Functions 笔记
http://yanbin.is-programmer.com/posts/94214.html Registery的Key 1. 整数Key用于Lua的引用机制,所以不要使用整数作为Key 2. 通 ...
- 【转】Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
转自:http://segmentfault.com/blog/hongliang/1190000000631630 关于cocos2d-x下Lua调用C++的文档看了不少,但没有一篇真正把这事给讲明 ...
- Step By Step(Lua调用C函数)
原文: http://www.cnblogs.com/stephen-liu74/archive/2012/07/23/2469902.html Lua可以调用C函数的能力将极大的提高Lua的可扩展性 ...
- cocos进阶教程(1)Lua调用自定义C++类和函数的最佳实践
第一层:纯C环境下,把C函数注册进Lua环境 a.lua 文件 )) a.c 文件 #include <lua.h> #include <lualib.h> #include ...
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践[转]
Cocos2d-x下Lua调用C++这事之所以看起来这么复杂.网上所有的文档都没讲清楚,是因为存在5个层面的知识点: 1.在纯C环境下,把C函数注册进Lua环境,理解Lua和C之间可以互相调用的本质 ...
- lua调用c++函数返回值作用
2015/05/28 lua调用c++接口,返回给lua函数的是压入栈的内容,可以有多个返回值.但是c++接口本身也是有返回值的,这个返回值也非常的重要,会决定最后返回到lua函数的值的个数. (1) ...
- lua入门之二:c/c++ 调用lua及多个函数返回值的获取
当 Lua 调用 C 函数的时候,使用和 C 调用 Lua 同样类型的栈来交互. C 函数从栈中获取她的參数.调用结束后将返回结果放到栈中.为了区分返回结果和栈中的其它的值,每一个 C 函数还会返回结 ...
- lua调用c函数
参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...
随机推荐
- uboot移植rtc
uboot中可能会有需求提供rtc的支持目的达到uboot启动也能够进行墙上时间的显示和后面推断.大部分rtc支持的一个必要条件就是已经有i2c的支持.由于非常多的rtc是i2c接口控制的.uboot ...
- JSF教程(8)——生命周期之Apply Request Values Phase
当一个组件树在一个postbacks请求中被恢复之后其中每一个组件从request的參数中取得各自的值,这里使用的是processDecodes方法. 这个值会保存在本地的每一个组件中,在源代码中此过 ...
- DOTA游戏相关的文章
DOTA里面到底有几号位?各代表什么? DOTA新手进阶之S.SHIFT及M键的使用 Dota开局 对线方法技巧总结 dota补刀的技巧 dota需要注意的小细节 dota前期如何对线 DotA 命令 ...
- vscode 编译调试c/c++的环境配置
首先看了一下别人写的文章 http://blog.csdn.net/c_duoduo/article/details/51615381 在按照上文链接博主的安装步骤进行到MINGW的安装时出现一个问题 ...
- [转]Linux shell中的那些小把戏
我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧.是的,我能记住一些命令,但是肯定不会只在特定的任务上使用一次,所以我就开始在我的Dropbox账号里用文本 ...
- 15:取近似值ApproximateValue
题目描述 写出一个程序,接受一个正浮点数值,输出该数值的近似整数值.如果小数点后数值大于等于5,向上取整:小于5,则向下取整. 输入描述:输入一个正浮点数值 输出描述:输出该数值的近似整数值 输入例子 ...
- hadoop权威指南学习
通常情况下,处理少量的大型文件更容易.更有效,为什么呢? map阶段中的键如果不需要可以忽略掉? MapReduce过程也可以用于本地文件的处理,但是如果是要使用到集群的话还需要HDFS. Data ...
- The Gray World Assumption
Color Constancy 色彩恒常性(2)The Gray World Assumption act=qbbkrzydb_20150408_01" style="line-h ...
- 【BZOJ2843】极地旅行社 离线+树链剖分+树状数组
[BZOJ2843]极地旅行社 Description 不久之前,Mirko建立了一个旅行社,名叫“极地之梦”.这家旅行社在北极附近购买了N座冰岛,并且提供观光服务.当地最受欢迎的当然是帝企鹅了,这些 ...
- c++对象内存的分配
1 关于c++的对象 只要是用了class或者struct定义的,都是对象,不管有没有方法.不过,一般情况下,没有方法的对象用struct关键字来定义. 2 不用new关键字定义对象 要看这样的对象在 ...