Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Code Compiler in C#

In C#, I am able to compile VB and C# Code, using the code below, but I have no way of compiling C/C++ code. Is there any way of doing this?

C# Compiler:

        public void Compile(string ToCompile)
        {
            string Result = null;
            string errors = null;
            Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
            System.CodeDom.Compiler.ICodeCompiler icc = codeProvider.CreateCompiler();
            string Output = @"mypath";
            System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = Output;
            System.CodeDom.Compiler.CompilerResults results = icc.CompileAssemblyFromSource(parameters, ToCompile);
            if (ReturnErrors == true)
            {
                if (results.Errors.Count > 0)
                {
                    foreach (System.CodeDom.Compiler.CompilerError CompErr in results.Errors)
                    {
                        errors +=
                                    "Line number " + CompErr.Line +
                                    ", Error Number: " + CompErr.ErrorNumber +
                                    ", '" + CompErr.ErrorText + ";" +
                                    Environment.NewLine + Environment.NewLine;
                    }
                    Result += "Errors have been found in your code: " + Environment.NewLine + errors;
                }
                else
                {
                    Result += "Success!";
                    System.Diagnostics.Process.Start(Output);
                }
            }

And to create a VB compiler, I simply replace Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); with Microsoft.VisualBasic.VBCodeProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider();

like image 253
Joe Avatar asked Nov 01 '13 16:11

Joe


2 Answers

You can compile c++/cli code, not native c++ as mentioned above. You can archive c++/cli compilation with CppCodeProvider (http://msdn.microsoft.com/en-us/library/microsoft.visualc.cppcodeprovider(v=vs.85).aspx) class using it like in your example.

like image 177
Redwan Avatar answered Sep 19 '22 10:09

Redwan


I'm assuming you've got a chunk of source, say containing a function with a known prototype, which you want to compile, and run within your currently running application.

In order to do this in native (not managed) C++, you'd need to do the following:

  • Dynamically store your code into a boilerplate dll source project (i.e. everything written with a gap for the function's code, which you'd insert)
  • Spawn a C++ compiler (which the end user would have to have pre-installed) to output a dll
  • Build up a C++/Cli wrapper that wraps the c++ dll that you built above, and compile that too (see Redwan's answer)
  • Dynamically load your wrapper Dll, and call the function.

If you can work with just managed c++/CLI, then Redwan's answer should be adequate on its own.

like image 35
benjymous Avatar answered Sep 22 '22 10:09

benjymous