C言語後方互換性
TerraはClangの機能を使って、C言語との後方互換性を実現しているので、C言語から使えるライブラリならTerraからも使うことができる。
試しにTcl/TkをTerraから使ってみる。
libtcl = "libtcl8.5.so.0"
libtk = "libtk8.5.so.0"
includepath = "/usr/include/tcl"
-- 動的ライブラリのリンク
terralib.linklibrary(libtcl)
terralib.linklibrary(libtk)
-- C インクルードファイルの読み込み
tcl = terralib.includec("tcl.h","-I",includepath)
tk = terralib.includec("tk.h","-I",includepath)
tcl_code = [[
button .hello -text {Hello, world!} -command exit
pack .hello
]]
terra hello()
var interp : &tcl.Tcl_Interp = tcl.Tcl_CreateInterp()
tcl.Tcl_Init(interp)
tk.Tk_Init(interp)
tcl.Tcl_Eval(interp, tcl_code)
tk.Tk_MainLoop()
end
terra main(argc : int, argv : &rawstring) : int
hello()
return 0
end
terralib.saveobj("hellotcltk.o", {hello = hello, main = main})
hello()これをterraで実行すると、Tkのウィンドウが表示される。また、hellotcltk.oというファイルが生成されるので、これをライブラリとリンクして実行ファイルを生成することもできる。
cc -o hellotcltk hellotcltk.o -ltcl -ltk