コード例 #1
0
        static void Main(string[] args)
        {
            string code = "#include <tcclib.h>\n" +                        /* include the "Simple libc header for TCC" */
                          "#define _cdecl __attribute__((__cdecl__))\n" +  /*define cdelc calling convention*/
                          "#define _import __attribute__((dllimport))\n" + /*define dllimport for symbol*/
                          "_cdecl _import extern int add(int a, int b);\n" +
                          "_import extern const char hello[];\n" +
                          "_cdecl int fib(int n)\n" +
                          "{\n" +
                          "    if (n <= 2)\n" +
                          "        return 1;\n" +
                          "    else\n" +
                          "        return fib(n-1) + fib(n-2);\n" +
                          "}\n" +
                          "\n" +
                          "_cdecl int foo(int n, const char* txt)\n" +
                          "{\n" +
                          "    printf(\"%s\\n\", hello);\n" +
                          "    printf(\"fib(%d) = %d\\n\", n, fib(n));\n" +
                          "    printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n" +
                          "    printf(\"param Text=%s\\n\",txt);\n" +
                          "    return 0;\n" +
                          "}\n";


            //Create a Compiler Object please use always USING
            //The Object must be disposed!!!
            using (var compiler = new TccCompiler())
            {
                //compiler.SetOutPutType(TccOutputType.Memory);
                //string fp = Path.Combine(Environment.CurrentDirectory, "Code.c");

                //code = File.ReadAllText(fp);



                //if (compiler.CompileString(code) == -1)
                //{
                //    Console.WriteLine("Could not comple");
                //    return;
                //}


                DelegatePtr bnClick = new OnClick(OnButtonClick);
                compiler.BeforeCompileAction = () =>
                {
                    compiler.AddSymbol("ButtonClick", bnClick);
                };

                compiler.CompilerErrorAction = ErrorFuncDel;
                //Waits until windows closed!
                compiler.Run("Code.c");

                bnClick.Dispose();

                //Add addDel = AddImp;
                //DelegatePtr addPtr = addDel;
                //int retVal = compiler.AddSymbol("add", addPtr);
                //if (retVal != 0)
                //{
                //    Console.WriteLine("Cannot add Smbol add!");
                //    addPtr.Dispose();
                //    return;
                //}


                //string hello = "hallo Welt";
                ////IntPtr halloPtr = Marshal.StringToHGlobalAnsi(hello);
                //HGlobalAnsiStringPtr halloPtr = hello;
                //compiler.AddSymbol("hello", halloPtr);

                //if (compiler.Reallocate(TccRealocateConst.TCC_RELOCATE_AUTO) < 0)
                //{
                //    Console.WriteLine("Could not reloacate the intern pointers");
                //    addPtr.Dispose();
                //    halloPtr.Dispose();
                //    return;
                //}

                //DelegatePtr<Foo> fooPtr = compiler.GetSymbol("foo");
                //if (!fooPtr.IsValid)
                //{
                //    Console.WriteLine("cannot get foo Symbol!");
                //    addPtr.Dispose();
                //    halloPtr.Dispose();
                //    return;
                //}

                //fooPtr.Delegate(32, "Eingabe Text1");
                //fooPtr.Delegate(23, "Eingabe Text2");

                //halloPtr.Dispose();
                //addPtr.Dispose();
            }
        }