7

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();

3
  • 3
    Are you asking if there is a way to compile C/C++ code from C#? Commented Nov 1, 2013 at 16:05
  • 2
    c# and vb are managed codes means which runs under CLR.but C/C++ code is called unmanaged code, i think marshalling is what you are looking for. Commented Nov 1, 2013 at 16:06
  • 1
    @Sudhakar No that really isn't what he's after. Managed C++ can be compiled to run on the CLR. Commented Nov 1, 2013 at 16:11

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Nice, I didn't know about that. So deleted my answer as it's plain wrong. Here's an English link to that class BTW: msdn.microsoft.com/en-us/library/…
@DavidArno My bad. Forgot about russian language of article. Updated my answer.
@Redwan - My Copy of Visual Studio 2010 does not have Microsoft.VisualC - What should I do?
@Joe this class is shipped within Windows SDK as mentioned in this article msdn.microsoft.com/en-us/library/…
@Redwan - I am going to download Windows SDK - Is this a good idea?
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.