简体   繁体   中英

How to compile C++ Code using CppCodeProvider in C#

I tried to compile C++ code in C# using CodeDomProvider but program is giving errors. I dont know exactly how to use CppClassProvider I cant find any reference material on Internet regarding CppCodeProvider, all the websites have CSharpCodeProvider or CodeDomProvder examples. I will be thankful if anyone can help me with the situation.

If I am going in wrong direction, I want to explain that I am interested in compiling C++ code in my C# application and build ".exe" as CSharpCodeProvider does for "C#" code.

The Code I was trying to implement is provided below. The code is written badly, please ignore GPPs(Good Programming Practices) as I was just doing this to try currently.

namespace CPPCodeProviderTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CompilerParameters cparams = new CompilerParameters();
            cparams.GenerateExecutable = true;
            String output = @"F:\test.exe";
            cparams.OutputAssembly = output;
            cparams.CompilerOptions = "/optimize";
          //CppCodeProvider cpp = new CppCodeProvider();
            CodeDomProvider pro = CodeDomProvider.CreateProvider("cpp");
            cparams.ReferencedAssemblies.Add("System.dll");
            String f = Properties.Resources.code;
            CompilerResults cr = pro.CompileAssemblyFromSource(cparams, f);
            if (cr.Errors.Count > 0)
            {
                foreach (CompilerError e in cr.Errors)
                {
                    Console.WriteLine(e.ErrorNumber + " " + e.ErrorText);
                }
            }
            else
                Console.WriteLine("successfull");
        }
    }
}

Resources.code

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!";
    return 0;
}

Error:

 System.NotImplementedException was unhandled
  HResult=-2147467263
  Message=The method or operation is not implemented.
  Source=CppCodeProvider
  StackTrace:
       at Microsoft.VisualC.CppCodeProvider.CreateCompiler()
       at System.CodeDom.Compiler.CodeDomProvider.CreateCompilerHelper()
       at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromSource(CompilerParameters options, String[] sources)
       at CPPCodeProviderTest.Program.Main(String[] args) in c:\Users\SSV\Documents\Visual Studio 2013\Projects\CPPCodeProviderTest\CPPCodeProviderTest\Program.cs:line 25
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Thanks :)

This method Microsoft.VisualC.CppCodeProvider.CreateCompiler is throwing an exception System.NotImplementedException .

This is thrown when an object implements an interface, but not all of the methods are implemented. Typically this occurs when it is documented that the method may not be implemented.

The documentation is here:

It says:

This method is obsolete in the .NET Framework 2.0. The recommended alternative is to call the ICodeCompiler methods that are directly available in the code provider.

Notes to Inheritors In the .NET Framework 2.0, you should implement the ICodeCompiler members in the CodeDomProvider class and throw a NotSupportedException when this method is called.

After a long digging, I found that Compilation of CPP is not supported by Microsoft Currently. You can use any C++ Compiler that supports command line (gcc, Mingw etc.) to compile your c++ code.

The strange thing is when you check the following code it returns true, but the method is not implemented by Microsoft.

CodeDomProvider.IsDefinedLanguage("Cpp")

If anyone find that I am wrong, or find any better way to do this, please let me know. Thanks :)

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