使用tcc编译器--开发已停止

来源:互联网 发布:单片机软件申请专利 编辑:程序博客网 时间:2024/05/22 16:53

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 plaincopy
  1. /* 
  2.  * Simple Test program for libtcc 
  3.  * 
  4.  * libtcc can be useful to use tcc as a "backend" for a code generator. 
  5.  */  
  6. #include <stdlib.h>  
  7. #include <stdio.h>  
  8. #include <string.h>  
  9.   
  10. #include "libtcc.h"  
  11.   
  12. /* this function is called by the generated code */  
  13. int add(int a, int b)  
  14. {  
  15.     return a + b;  
  16. }  
  17.   
  18. char my_program[] =  
  19. "int fib(int n)\n"  
  20. "{\n"  
  21. "    if (n <= 2)\n"  
  22. "        return 1;\n"  
  23. "    else\n"  
  24. "        return fib(n-1) + fib(n-2);\n"  
  25. "}\n"  
  26. "\n"  
  27. "int MsgBox()\n"  
  28. "{"  
  29. "    return MessageBoxA(0,\"Hello world!\",\"Title\",0);\n"  
  30. "}"  
  31. "\n"  
  32. "int foo(int n)\n"  
  33. "{\n"  
  34. "    printf(\"Hello World!\\n\");\n"  
  35. "    printf(\"fib(%d) = %d\\n\", n, fib(n));\n"  
  36. "    printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"  
  37. "    return 0;\n"  
  38. "}\n";  
  39.   
  40. int main(int argc, char **argv)  
  41. {  
  42.     TCCState *s;  
  43.     int (*func)(int);  
  44.     int (*MyFunc)();  
  45.     void *mem;  
  46.     int size;  
  47.   
  48.     s = tcc_new();  
  49.     if (!s) {  
  50.         fprintf(stderr, "Could not create tcc state\n");  
  51.         exit(1);  
  52.     }  
  53.     /* if tcclib.h and libtcc1.a are not installed, where can we find them */  
  54.     if (argc == 2 && !memcmp(argv[1], "lib_path=",9))  
  55.         tcc_set_lib_path(s, argv[1]+9);  
  56.   
  57.     /* MUST BE CALLED before any compilation */  
  58.     tcc_set_output_type(s, TCC_OUTPUT_MEMORY);  
  59.     tcc_add_library(s, "user32");  
  60.   
  61.     if (tcc_compile_string(s, my_program) == -1)  
  62.         return 1;  
  63.   
  64.     /* as a test, we add a symbol that the compiled program can use. 
  65.        You may also open a dll with tcc_add_dll() and use symbols from that */  
  66.     tcc_add_symbol(s, "add", add);  
  67.   
  68.     /* get needed size of the code */  
  69.     size = tcc_relocate(s, NULL);  
  70.     if (size == -1)  
  71.         return 1;  
  72.   
  73.     /* allocate memory and copy the code into it */  
  74.     mem = malloc(size);  
  75.     tcc_relocate(s, mem);  
  76.   
  77.     /* get entry symbol */  
  78.     func = tcc_get_symbol(s, "foo");  
  79.     if (!func)  
  80.         return 1;  
  81.   
  82.     MyFunc = tcc_get_symbol(s, "MsgBox");  
  83.   
  84.     /* delete the state */  
  85.     tcc_delete(s);  
  86.     MyFunc();  
  87.     /* run the code */  
  88.     func(32);  
  89.   
  90.     free(mem);  
  91.     return 0;  
  92. }  

编译: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文件控制。


0 0
原创粉丝点击