如何在C#中使用CppCodeProvider编译C ++代码

时间:2014-05-08 21:48:07

标签: c# c++ codedom

我尝试使用CodeDomProvider在C#中编译C ++代码,但程序出错。我不确切知道如何使用CppClassProvider我无法在Internet上找到有关CppCodeProvider的任何参考资料,所有网站都有CSharpCodeProvider或CodeDomProvder示例。如果有人能帮我解决这个问题,我将感激不尽。

如果我的方向错误,我想解释一下,我有兴趣在我的C#应用​​程序中编译C ++代码并构建" .exe"正如CSharpCodeProvider为" C#"代码。

我试图实施的准则如下。代码编写得很糟糕,请忽略GPP(良好编程实践),因为我目前正在尝试这样做。

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;
}

错误:

 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: 

谢谢:)

2 个答案:

答案 0 :(得分:2)

此方法Microsoft.VisualC.CppCodeProvider.CreateCompiler抛出异常System.NotImplementedException

当对象实现接口但未实现所有方法时抛出此异常。通常,当记录该方法可能未实现时,会发生这种情况。

文档在这里:

它说:

  

此方法在.NET Framework 2.0中已过时。推荐   另一种方法是直接调用ICodeCompiler方法   可在代码提供商处获得。

     

对继承者的说明在.NET Framework 2.0中,您应该实现   CodeDomProvider类中的ICodeCompiler成员并抛出一个   调用此方法时出现NotSupportedException。

答案 1 :(得分:1)

经过长时间的挖掘,我发现Microsoft目前不支持CPP编译。您可以使用任何支持命令行(gcc,Mingw等)的C ++编译器来编译您的c ++代码。

奇怪的是,当您检查以下代码时,它返回true,但该方法未由Microsoft实现。

CodeDomProvider.IsDefinedLanguage("Cpp")

如果有人发现我错了,或者找到更好的办法,请告诉我。 谢谢:)

相关问题