使用tcc編譯器 開發已停止

2021-07-06 05:48:45 字數 2598 閱讀 6853

tcc是純c編譯器,支援c99標準,有些獨到特點。

1. tcc的主頁(已停止開發)

2. 編譯tcc

gcc -o2 -shared -wall -wl,--export-all-symbols -mpreferred-stack-boundary=2 -march=i386 -falign-functions=0 -fno-strict-aliasing -dtcc_target_pe -dlibtcc -o libtcc.dll tcc.c

或者tcc -shared -rdynamic -o libtcc.dll tcc.c #這樣會匯出所有函式,有些亂。

tcc -shared libtcc.c -dlibtcc_as_dll -dtcc_target_pe -o libtcc.dll -ic:\tcc\include\winapi -i c:\tcc\include -i. -lc:\tcc\lib #這樣產生的動態鏈結庫匯出表函式正常。

3.嵌入(示例:test.c)

# define tcc_target_pe

[cpp]view plain

copy

/** ****** test program for libtcc

** libtcc can be useful to use tcc as a "backend" for a code generator.

*/#include 

#include 

#include 

#include "libtcc.h"

/* this function is called by the generated code */

intadd(

inta, 

intb)  

char

my_program =  

"int fib(int n)\n"

"\n"

"\n"

"int msgbox()\n"

"""\n"

"int foo(int n)\n"

"\n"

;  int

main(

intargc, 

char

**argv)  

/* if tcclib.h and libtcc1.a are not installed, where can we find them */

if(argc == 2 && !memcmp(argv[1], 

"lib_path="

,9))  

tcc_set_lib_path(s, argv[1]+9);  

/* must be called before any compilation */

tcc_set_output_type(s, tcc_output_memory);  

tcc_add_library(s, "user32"

);  

if(tcc_compile_string(s, my_program) == -1)  

return

1;  

/* as a test, we add a symbol that the compiled program can use.

you may also open a dll with tcc_add_dll() and use symbols from that */

tcc_add_symbol(s, "add"

, add);  

/* get needed size of the code */

size = tcc_relocate(s, null);  

if(size == -1)  

return

1;  

/* allocate memory and copy the code into it */

mem = malloc(size);  

tcc_relocate(s, mem);  

/* get entry symbol */

func = tcc_get_symbol(s, "foo"

);  

if(!func)  

return

1;  

myfunc = tcc_get_symbol(s, "msgbox"

);  

/* delete the state */

tcc_delete(s);  

myfunc();  

/* run the code */

func(32);  

free(mem);  

return

0;  

}  

編譯:tcc -o test.exe test.c libtcc.def

執行:test.exe lib_path=c:\tcc (內含lib目錄:libtcc1.a, kernel32.def, msvcrt.def) 本例還需user32.def (tiny_impdef user32.dll -o user32.def)

4. 評價

優點:小巧,編譯速度快,可內嵌(這點很棒)。

缺點:開發已停止,匯出函式列表無法用def檔案控制。

編譯器開發(一)

好吧。在學習完編譯原理之後終於自己做了乙個編譯器的前端。當然只是乙個很簡單的前端。前端分為詞法分析,語法分析和語義分析。語法的結構如下 程式 main 語句塊 語句塊 語句串 語句 語句 賦值語句 條件語句 迴圈語句 賦值語句 id 表示式 條件語句 if 條件 語句塊 else 語句塊 迴圈語句 ...

QtCreator MSVC編譯器開發

qt在windows中的編譯,一般使用兩種編譯器,msvc和mingw mingw minimalist gnu for windows 是乙個gcc和gnu binutils的原生軟體埠,用於在windows上開發原生的microsoft windows應用程式。mingw和qt creator以...

GCC編譯器的使用

看下面的例子 test.c include main char str i like linux i advices you jion in the linux world printf s n str exit 0 使用gcc編譯 輸入gcc c test.c得到目標檔案test.o。c命令表示對...