[英]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 ++中执行此操作,您需要执行以下操作:
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.