简体   繁体   English

C#中的C / C ++代码编译器

[英]C/C++ Code Compiler in C#

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. 在C#中,我能够使用下面的代码编译VB和C#代码,但我无法编译C / C ++代码。 Is there any way of doing this? 有没有办法做到这一点?

C# Compiler: C#编译器:

        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(); 并且要创建一个VB编译器,我只需要替换Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); with Microsoft.VisualBasic.VBCodeProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider(); Microsoft.VisualBasic.VBCodeProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider();

You can compile c++/cli code, not native c++ as mentioned above. 您可以编译c ++ / cli代码,而不是如上所述的本机c ++。 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. 您可以使用CppCodeProvider( http://msdn.microsoft.com/en-us/library/microsoft.visualc.cppcodeprovider (v=vs.85) .aspx )类来存档c ++ / cli编译,就像在您的示例中一样。

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: 为了在本机(非托管)C ++中执行此操作,您需要执行以下操作:

  • Dynamically store your code into a boilerplate dll source project (ie everything written with a gap for the function's code, which you'd insert) 将代码动态存储到样板dll源项目中(即,所有用函数代码写入间隙的内容,您要插入的代码)
  • Spawn a C++ compiler (which the end user would have to have pre-installed) to output a dll 生成一个C ++编译器(最终用户必须预先安装)来输出一个dll
  • Build up a C++/Cli wrapper that wraps the c++ dll that you built above, and compile that too (see Redwan's answer) 构建一个C ++ / Cli包装器,它包装你在上面构建的c ++ dll,并编译它(参见Redwan的回答)
  • Dynamically load your wrapper Dll, and call the function. 动态加载包装器Dll,并调用该函数。

If you can work with just managed c++/CLI, then Redwan's answer should be adequate on its own. 如果你可以使用托管的c ++ / CLI,那么Redwan的答案本身应该足够了。

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

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