如何编译C ++代码使用CppCodeProvider在C# [英] How to compile C++ Code using CppCodeProvider in C#

查看:1122
本文介绍了如何编译C ++代码使用CppCodeProvider在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用CodeDomProvider在C#中编译C ++代码,但程序提供错误。我不知道如何使用CppClassProvider我没有找到任何参考资料在互联网上关于CppCodeProvider,所有的网站都有CSharpCodeProvider或CodeDomProvder示例。我会感激的,如果有人可以帮助我的情况。

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.

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

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.

我试图实现的代码如下。代码写得很糟,请忽略GPP(好的编程实践),因为我只是这样做目前尝试。

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

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: 

感谢:) Microsoft.VisualC。


Thanks :)

推荐答案

CppCodeProvider.CreateCompiler 正在抛出异常 System.NotImplementedException

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.

文档在这里:

  • http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.createcompiler(v=vs.110).aspx

它说:


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

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.

Inheritors的注意事项在.NET Framework 2.0 ,您应该实现
的CodeDomProvider类中的ICodeCompiler成员,并在调用此方法时抛出
NotSupportedException。

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.

这篇关于如何编译C ++代码使用CppCodeProvider在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆