简体   繁体   中英

Programatically compile the C code in c# windpws forms using CodeDom?

I am working on making a C compiler, I have to know if I can compile C code in c# using CodeDom, currently I am working wd following code that compiles the C# code in c# windows form?

Is there any easy way to compile C language's Code?

using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;
private void button1_Click(object sender, System.EventArgs e)
{
   CSharpCodeProvider codeProvider = new CSharpCodeProvider();
   ICodeCompiler icc = codeProvider.CreateCompiler();
   string Output = "Out.exe";
   Button ButtonObject = (Button)sender;

   textBox2.Text = "";
   System.CodeDom.Compiler.CompilerParameters parameters = new 
   CompilerParameters();
   //Make sure we generate an EXE, not a DLL
   parameters.GenerateExecutable = true;
   parameters.OutputAssembly = Output;
   CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);

   if (results.Errors.Count > 0)
   {
       textBox2.ForeColor = Color.Red;
       foreach (CompilerError CompErr in results.Errors)
       {
           textBox2.Text = textBox2.Text +
                       "Line number " + CompErr.Line +
                       ", Error Number: " + CompErr.ErrorNumber +
                       ", '" + CompErr.ErrorText + ";" +
                       Environment.NewLine + Environment.NewLine;
       }
   }
   else
   {
       //Successful Compile
       textBox2.ForeColor = Color.Blue;
       textBox2.Text = "Success!";
       //If we clicked run then launch our EXE
       if (ButtonObject.Text == "Run") Process.Start(Output);
   }
}

Your not making a compiler. You're making an interface to a compiler. No, you can't use CodeDom for this. Why not just shell out to a C compiler? There are tons you could use for this purpose. Capture STDOUT - there are general guidelines to the format of compiler outputs and parsing them should be a rather simple task.

You could also try looking into embedding a C interpreter. Now that could be interesting. Even more interesting: Write your own C interpreter (easier to pull off than a compiler).

I think you are following - How to programmatically compile code using C# compiler .

Before using arbitrary code for your work go through the whole that whether it is suitable to your requirement or not. that you are using is .net framework internal compilation helper classes. check the following lines of that article.

The .NET Framework exposes classes that allow you to programmatically access the C# language compiler. This might be useful if you want to write your own code-compiling utilities.

and requirement

  • Visual Studio
  • Visual C# language compiler

then how can you compile c program using these .net library class.

You can use the c compiler exe and using that compiler you can compile the files in console application or using the Process class in .net.

you can compile c from command line, just use the same from c# the redirectstandardoutput will allow you to get the output that would be displayed as well.

Process proc = new Process();
proc.StartInfo.FileName = "compiler name";
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM