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:


tcc -o shell.exe -L$(LIBDIR) -lc $(LIBDIR)/crt0.o shell.o
instead of:
tcc -o shell.exe -L$(LIBDIR) $(LIBDIR)/crt0.o shell.o -lc

我使用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 库。


原创粉丝点击