luajit笔记---编译成静态库以及FFI绑定宿主程序函数
2016-04-11 11:21
357 查看
http://blog.csdn.net/fg5823820/article/details/8888207
本以为可以像lua一样把代码丢进去直接编译就好了,结果发现luajit有一堆汇编代码,不知道怎么处理,后来一搜索才知道luajit本身提高的批处理也可以编译成静态库,就是在后面加个static,郁闷到了。http://blog.csdn.net/whitehack/article/details/6451293
Google来Google,终于看到用FFI绑定宿主程序函数的例子,卧槽,知道真相我的眼泪都流下来!原来FFI本质是绑定导出的符号,所以说只要导出符号就可以用,吐血。
[cpp] view
plain copy
#include <lua.hpp>
#include <cassert>
// Please note that despite the fact that we build this code as a regular
// executable (exe), we still use __declspec(dllexport) to export
// symbols. Without doing that FFI wouldn't be able to locate them!
extern "C"
{
__declspec(dllexport) void __cdecl hello_from_lua(const char *msg)
{
printf("A message from LUA: %s\n", msg);
}
__declspec(dllexport) int Add(int a,int b)
{
return a+b;
}
}
const char *lua_code =
"local ffi = require('ffi') \n"
"ffi.cdef[[ \n"
"const char * hello_from_lua(const char *); \n" // matches the C prototype
"int Add(int,int);"
"]] \n"
"ffi.C.hello_from_lua('Hello from LUA!') \n" // do actual C call
"sum = ffi.C.Add(10,20) \n"
"print('sum:'..sum) \n"
;
int main()
{
lua_State *lua = luaL_newstate();
assert(lua);
luaL_openlibs(lua);
const int status = luaL_dostring(lua, lua_code);
if(status)
printf("Couldn't execute LUA code: %s\n", lua_tostring(lua, -1));
lua_close(lua);
return 0;
}
另外发现,就算是导出了符号,FFI也只能对本程序的导出符号进行绑定,比如我在一个DLL里面导出了符号,虽然宿主程序使用了这个DLL,但是依然没法绑定。
后来看了luajit官网,发现lua51.dll里面的函数在FFI是可用的,至于他们怎么做到我就不清楚了,只能说有可能可以做到这一点。这种黑科技果然不是那么热就能掌握的,而且关于luajit,感觉资料实在太少了,说实在其实lua 的资料本身就少了
本以为可以像lua一样把代码丢进去直接编译就好了,结果发现luajit有一堆汇编代码,不知道怎么处理,后来一搜索才知道luajit本身提高的批处理也可以编译成静态库,就是在后面加个static,郁闷到了。http://blog.csdn.net/whitehack/article/details/6451293
Google来Google,终于看到用FFI绑定宿主程序函数的例子,卧槽,知道真相我的眼泪都流下来!原来FFI本质是绑定导出的符号,所以说只要导出符号就可以用,吐血。
[cpp] view
plain copy
#include <lua.hpp>
#include <cassert>
// Please note that despite the fact that we build this code as a regular
// executable (exe), we still use __declspec(dllexport) to export
// symbols. Without doing that FFI wouldn't be able to locate them!
extern "C"
{
__declspec(dllexport) void __cdecl hello_from_lua(const char *msg)
{
printf("A message from LUA: %s\n", msg);
}
__declspec(dllexport) int Add(int a,int b)
{
return a+b;
}
}
const char *lua_code =
"local ffi = require('ffi') \n"
"ffi.cdef[[ \n"
"const char * hello_from_lua(const char *); \n" // matches the C prototype
"int Add(int,int);"
"]] \n"
"ffi.C.hello_from_lua('Hello from LUA!') \n" // do actual C call
"sum = ffi.C.Add(10,20) \n"
"print('sum:'..sum) \n"
;
int main()
{
lua_State *lua = luaL_newstate();
assert(lua);
luaL_openlibs(lua);
const int status = luaL_dostring(lua, lua_code);
if(status)
printf("Couldn't execute LUA code: %s\n", lua_tostring(lua, -1));
lua_close(lua);
return 0;
}
另外发现,就算是导出了符号,FFI也只能对本程序的导出符号进行绑定,比如我在一个DLL里面导出了符号,虽然宿主程序使用了这个DLL,但是依然没法绑定。
后来看了luajit官网,发现lua51.dll里面的函数在FFI是可用的,至于他们怎么做到我就不清楚了,只能说有可能可以做到这一点。这种黑科技果然不是那么热就能掌握的,而且关于luajit,感觉资料实在太少了,说实在其实lua 的资料本身就少了
相关文章推荐
- luajit ffi 访问C动态库
- 项目记录34--tolua 框架 场景管理BaseScene
- Lua面向对象
- Lua进阶
- [Paper Reading] The Implementation of Lua 5.0
- Lua函数
- Lua语句
- Lua表达式
- Evaluate Reverse Polish Notation
- Metatable让我从心认知了Lua(相知篇)
- Lua基础
- LuaFileSystem学习心得
- lua中table如何安全移除元素
- lua编译环境搭建
- 项目记录33--tolua 框架 回顾架构和重构事件派发LNotice
- 项目记录32--tolua build android错误tolua.lua no found
- lua 表赋值是引用关系
- Lua学习笔记-习题5.4
- Lua中的loadfile,dofile,require使用,最后还有调试
- Lua语法基础(3)--迭代器和泛型for