lua使用 tinycc
来源:互联网 发布:开淘宝店用电脑配置 编辑:程序博客网 时间:2024/05/17 23:58
1.下载tinycc
http://bellard.org/tcc/
http://repo.or.cz/w/tinycc.git 最新的
2.编译tcc
需要mingw
进入 tinycc/win32
运行build-tcc.bat
win32/libtcc 下面是 libtcc.a 和 libtcc.h
win32 下面是tcc.exe 和 libtcc.dll
include 和lib 目录是 tcc.exe所需要的c运行时库和头文件,必须与tcc.exe在同个目录。
libtcc.dll也必须与tcc.exe在同个目录。
linux下x86_64平台,tcc编译.a需要使用-fPIC,因为lua库luatcclib.so是share object需要链接。
3.使用tcc
程序 表示使用tcc的主程序,代码 表示程序用tcc动态编译的对象。
a.
如果是程序使用libtcc.dll或libtcc.a,那么这个程序同级目录下也需要有 include lib目录 以及 libtcc.dll。
不然就报找不到cruntime头文件定义错误,以及 cannot find library: tcc1的错误。
或者通过tcc_set_lib_path设置含有lib和include文件夹的目录。必须在tcc_new()后 tcc_set_output_type前马上设置。
b.
如果程序使用vs 编译,需要根据 win32/lib/libtcc.def 生成 libtcc.lib,拷到libtcc目录下。命令如下:
lib /machine:ix86 /def:libtcc.def /out:libtcc.lib
这个程序如果使用mingw编译,那么使用libtcc.a就可以链接了。
c.cruntime 头文件
代码中不用加 include "stdio.h"等c runtime头文件。
d.代码使用lib
使用tcc_add_include_path tcc_add_library_path 添加路径。
代码使用的库的格式和mingw的gcc一样需要.a而不能是.lib。
tcc_add_library(xxx) 添加链接库libxxx.a。
http://forum.osdev.org/viewtopic.php?f=13&t=24703
After a lot of experimenting, I finally found a way to make my linker happy:
我使用liblua.a还是没链接成功,还是找不到符号,只能换dll。
e.代码使用dll
vs使用 dll对应的lib,gcc使用dll对应的a,而tcc使用的是dll对应的def。
How to link
tiny_impdef.exe xxx.dll 生成 xxx.def。
把def文件放到库路径中。
dll还没试过,要么放库路径,要么和libtcc.dll一样是放程序同目录。
所以代码因为要做lua接口c module,就需要使用 lua52.dll,可以采用此方法链接。
4.下载luatcc
http://luatcc.luaforge.net/
http://piratery.net/hg/luatcc/
5.修改luatcc.c
a.loader.lua 改名为 luatcc.lua ,本文件名改为luatcclib.c。仿照luaxml库,为lua包和c module名字上作区分。
b.为了迁移到 Lua 5.2。
luaL_register 改为luaL_newlib
lua_getfenv 改为 lua_getuservalue
lua_setfenv改为 lua_setuservalue
c.删除lua__new函数中lua_newtable(L); /* __index */这行
d. tcc相对于0.9.25, tcc_relocate函数的改动
新的LIBTCCAPI int tcc_relocate(TCCState *s1);
老的LIBTCCAPI int tcc_relocate(TCCState *s1, int size);
老的接口在tcc_relocate_ex中,不作为库接口。不需要自己管理代码remap的空间生成和释放了, 好像0.9.23以前就是这种做法。
所以lua__tcc__relocate函数中,把原来23前的代码拿来用就行了。
e.库接口
luaopen_module函数名改为 luaopen_luatcclib。不然就报error loading module “XXX” from file
6.修改loader.lua
a.loader.lua 改名为 luatcc.lua
b.
--module(..., package.seeall)
--local luatcc = require(_PACKAGE:sub(1,-2))
local luatcc = require 'luatcclib'
c.最后加上return luatcc
d.search函数是用来动态编译加载c文件的,可以当lua库用。
7. lua2c
把lua代码转为c
测试代码 lua2c没整进来自动使用,在外部转好后拷过来的:
local luatcc = require 'luatcc'local context = luatcc.new_context()context:compile([[#include <lua.h>#include <lauxlib.h>#include <lualib.h>static void lc_mul(lua_State * L, int idxa, int idxb) { if (lua_isnumber(L,idxa) && lua_isnumber(L,idxb)) { lua_pushnumber(L,lua_tonumber(L,idxa) / lua_tonumber(L,idxb)); } else { if (luaL_getmetafield(L,idxa,"__mul")||luaL_getmetafield(L,idxb,"__mul")) { lua_pushvalue(L,idxa < 0 && idxa > LUA_REGISTRYINDEX ? idxa-1 : idxa); lua_pushvalue(L,idxb < 0 && idxb > LUA_REGISTRYINDEX ? idxb-2 : idxb); lua_call(L,2,1); } else { luaL_error(L, "attempt to perform arithmetic"); } }}/* name: mul * function(a, b) */int lcf1_mul (lua_State * L) { enum { lc_nformalargs = 2 }; lua_settop(L,2); /* return a * b */ lc_mul(L,1,2); return 1;}]])context:relocate()local mul = context:get_symbol("lcf1_mul")print(mul(2, 3))
整个过程应该是: 主程序 -> 使用lua 库载入lua程序 -> lua程序 使用lua2c将 lua代码转为c -> lua程序使用luatcc库编译c代码 -> luatcc使用 tcc库 编译代码 -> 编译的代码需要链接到 lua 库。
- lua使用 tinycc
- 小玩TinyCC (aka tcc)
- lua使用
- lua 函数 使用 lua 函数 高级使用
- LUA使用笔记
- 学习Lua,使用devcpp
- 使用MinGW编译Lua
- Lua使用心得(1)
- Lua使用心得(2)
- 使用luaplus...调用lua
- Lua使用入门
- Lua使用入门
- Lua 脚本的使用
- lua使用相关文档
- Lua使用心得一
- Lua使用心得二
- Lua安装与使用
- lua脚本使用
- java 兼容问题
- android平台下使用点九PNG技术
- no declaration can be found for element 'aop:config'
- c# 存取结构体二进制文件
- Linux目录权限
- lua使用 tinycc
- yii使用CUploadedFile上传文件的一般方法
- android中的一些开源项目
- Response.ContentType 详细列表
- C++中的基础问题——virtual destructor问题,new与malloc区别
- 模态对话框和非模态对话框的消息循环
- 个人学习1之-DATETIME数据类型】 .
- android Application类的详细介绍 .
- DLL编程的导入导出,__declspec(dllimport),__declspec(dllexport)