繁体   English   中英

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

[英]How to compile C++ Code using CppCodeProvider in C#

我试图使用CodeDomProvider在C#中编译C ++代码,但是程序给出了错误。 我不知道如何使用CppClassProvider。我在Internet上找不到任何有关CppCodeProvider的参考资料,所有网站上都有CSharpCodeProvider或CodeDomProvder示例。 如果有人可以帮助我解决我,我将不胜感激。

如果我走错了方向,我想解释一下我有兴趣在C#应用程序中编译C ++代码,并像CSharpCodeProvider为“ C#”代码那样构建“ .exe”。

下面提供了我尝试实现的代码。 该代码写得不好,请忽略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");
        }
    }
}

资源代码

#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: 

谢谢 :)

此方法Microsoft.VisualC.CppCodeProvider.CreateCompiler引发异常System.NotImplementedException

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

文档在这里:

它说:

在.NET Framework 2.0中,此方法已过时。 建议的替代方法是调用在代码提供程序中直接可用的ICodeCompiler方法。

对继承者的说明在.NET Framework 2.0中,应在CodeDomProvider类中实现ICodeCompiler成员,并在调用此方法时引发NotSupportedException。

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

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

CodeDomProvider.IsDefinedLanguage("Cpp")

如果有人发现我错了,或者找到任何更好的方法来做到这一点,请告诉我。 谢谢 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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