**Tiny C Compiler (TCC)** 是一個極小、極快的 C 編譯器,由 Fabrice Bellard 編寫,支持 C90 及部分 C99、GNU 擴展。特點:
# 編譯生成可執行文件 tcc hello.c -o hello # 直接編譯運行(腳本模式) tcc -run hello.c # 編譯多文件 tcc main.c utils.c -o app # 指定輸出目錄 tcc -o out/prog prog.c # 顯示版本 tcc -v
// 宿主程序動態編譯運行 C 代碼字符串 #includeTCCState *s = tcc_new(); tcc_set_output_type(s, TCC_OUTPUT_MEMORY); tcc_compile_string(s, "int add(int a,int b){return a+b;}"); tcc_relocate(s, TCC_RELOCATE_AUTO); int (*add)(int,int) = tcc_get_symbol(s, "add"); printf("%d\n", add(1,2)); // 3 tcc_delete(s);
tcc_add_symbol(s, "my_print", my_print_func);
tcc_compile_string(s, "void test(){ my_print(\"from script\"); }");
標準庫頭文件需安裝 `tcc` 開發包或指定 `-I` 路徑。
TCC 是**「小而美」**編譯器的典範。不適合發布構建,但**開發調試、嵌入腳本、極簡構建**場景無可替代。建議開發者機器常備 `tcc`,配合 `tcc -run` 秒級驗證算法邏輯、測試 API 用法。