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 ...
随机推荐
- linux下内存
MMU由一个或一组芯片组成.其功能是把逻辑地址映射为物理地址,进行地址转换(MMU是CPU的一部分) 机器指令仍然用逻辑地址指定一个操作数的地址或一条指令的地址 每个逻辑地址都由一个段选择符(16位) ...
- Solidworks如何开启自动求解
1 我打开了一个DWG格式的文件,提示草图太大,关闭的自动求解(就是没法撤销了) 2 工具-草图设定-自动求解 3 随便做一个标注,然后发现撤销按钮可以使用了,点击之后也的确可以撤销刚才的操作 ...
- php中session的理解
一.Session是什么 Session一般译作会话,牛津词典对其的解释是进行某活动连续的一段时间.从不同的层面看待session,它有着类似但不完全同样的含义.比方,在web应用的用户看来,他打开浏 ...
- Archlinux 下的 VMWare Workstation 维护笔记
印象中 Archlinux 下的 VMWare Workstation 总是出问题, 因此写这个帖子, 记录出问题时间/原因/解决方案. PS: 每次更新内核后可能需要重新编译 vmware 的内核模 ...
- boost库shared_ptr实现桥接模式
主程序 /*将抽象部分与实现部分分离,使它们都能够独立的变化*/ #include "bridge.h" int main() { cout <<"main ...
- 从0开始学习 GitHub 系列汇总笔记
本文学习自Stromzhang, 原文地址请移步:从0开始学习 GitHub 系列汇总 我的笔记: 0x00 从0开始学习GitHub 系列之[初识GitHub] GitHub 影响力 a.全球顶级 ...
- Bootstrap学习速查表(二) 排版及表格
一.h1~h6标签 固定不同级别标题字体大小,h1=36px,h2=30px,h3=24px,h4=18px,h5=14px和h6=12px. 1.重新设置了margin-top和margin-bot ...
- DataTable去除空行
protected void removeEmpty(DataTable dt) { List<DataRow> removelist = new List<DataRow>( ...
- php nginx超时出错
执行PHP操作大文件insert mysql数据库时,出现这个错误提示 The page you are looking for is temporarily unavailable.Please t ...
- mongo的时间类型,erlang中对其的处理
需求:要想在一个调度中,从mongo中查出大于一个时间戳的所有的数据总和. 这个需求很简单,一个是scheduler,还有另一个就是查出来大于某个时间戳的总和,比如大于每天0点时间点的和. 需要注意的 ...