TCC4Java是TinyCC(也叫做TCC,一个神奇的C编译器)的Java绑定,让Java支持使用C作为脚本语言。
TCC4Java支持以下操作系统:
Windows x86/x64 Linux x86/x64/arm Mac OS X Debian kFreeBSD x86/x64 FreeBSD x86/x64 NetBSD x86/x64 DragonFlyBSD x86/x64
在以下系统中测试过:
Windows XP SP3 x86 Windows 7 SP1 x86 Ubuntu 12.04 x86/x64 Debian Wheezy Raspbian 2014-01-07 Mac OS X 10.7 Debian kFreeBSD 7.5.0 x86/x64 FreeBSD 9.0 x86/x64 (The FreeBSD based os such as GhostBSD, PC-BSD, ArchBSD, MidnightBSD are also supported) NetBSD 6.1.4 x86/x64 DragonFlyBSD 3.6.2 x86/x64
简单的例子:
import java.io.IOException; import cn.com.tcc.State; import cn.com.tcc.TCC; public class HelloTCC { public static void main(String[] args) throws IOException { TCC.init("./tcc/"); State state = new State(); state.compileString("int sum(int a, int b) {return a + b;}\n" + "int main(int argc, char* argv[]) {printf(\"sum(%d, %d) = %d\\n\", atoi(argv[1]), atoi(argv[2]), sum(atoi(argv[1]), atoi(argv[2]))); return 0;}"); state.run("", 2, 3); state.delete(); } }
TCC.init()初始化TCC,参数为包含TCC动态链接库和头文件的目录。
state.compileString()接受一个包含C程序的字符串作为参数,编译它,state.run()运行编译后的程序,并传递参数2和3用于计算它们的和。程序输出为:
sum(2, 3) = 5
更多例子请查看https://github.com/wangzhengbo/TCC4Java中的examples和test.