我们知道,lua通过lua_State堆栈可以很方便的与C语言进行交互
http://blog.csdn.net/sm9sun/article/details/68946343
也可以调用专门为lua调用而封装的C库。
具体步骤:
1.原C文件中引入lua相关头文件
#include "lua.h"#include "lualib.h"#include "lauxlib.h"
2.声明调用函数
static int c_hello(lua_State *L) //参数一律为lua_State堆栈
3.注册一个全局函数映射
static const luaL_reg functions[] = {{"hello", c_hello},{0,0}};
4.open函数(主函数)
int luaopen_*(lua_State *L)/#include#include #include #define LUA_FILE "event_constants.lua"int main() {FILE *f = fopen(LUA_FILE, "w+");fprintf(f, "-- Generated by Alien constantsnn");fprintf(f, "%s = %in", "EV_SIZE ", sizeof(struct event));fprintf(f, "%s = %in", "EV_READ", EV_READ);fprintf(f, "%s = %in", "EV_WRITE", EV_WRITE);fprintf(f, "%s = %in", "EV_TIMEOUT", EV_TIMEOUT);fprintf(f, "%s = %in", "EVLOOP_NONBLOCK", EVLOOP_NONBLOCK);fprintf(f, "%s = %in", "EVLOOP_ONCE", EVLOOP_ONCE);fclose(f);}哪些在编译和运行时在Linux / Intel系统上生成此文件:-- Generated by Alien constantsEV_SIZE = 84EV_READ = 2EV_WRITE = 4EV_TIMEOUT = 1EVLOOP_NONBLOCK = 2EVLOOP_ONCE = 1这些步骤(生成C文件,编译,生成Lua文件)最好在扩展的构建步骤中完成。



