如何使用C#编译C ++代码。需要开发一个C ++ IDE

时间:2014-05-08 13:26:51

标签: c# c++ gcc compiler-construction

我对开发C ++ IDE感兴趣,但为了方便和更好的UI,我想用C#开发IDE。但我遇到了一个问题,我无法找到如何使用C#编译C ++代码并构建" .exe"使用我的IDE编译的C ++程序。任何人都可以建议我,如果他们是任何可以构建" .exe"的API或库。我提供输入的任何C ++代码。

我能够编译和构建C#代码,但是如果有一种方法可以在我的应用程序中集成c ++编译器功能,那么任何人都可以帮助我。 我需要的是类似于CSharpCodeProvider但是用于编译C ++代码。

我正在为C#编译做的是在下面的代码中。

class Compiler
{
    public static bool CompileFromSource(string source, string Output, string Icon = null, string[] Resources = null)
    {
        // We declare the new compiler parameters variable
        // that will contain all settings for the compilation.
        CompilerParameters CParams = new CompilerParameters();

        // We want an executable file on disk.
        CParams.GenerateExecutable = true;
        // This is where the compiled file will be saved into.
        CParams.OutputAssembly = Output;

        // We need these compiler options, we will use code optimization,
        // compile as a x86 process and our target is a windows form.
        // The unsafe keyword is used because the stub contains pointers and
        // unsafe blocks of code.
        string options = "/optimize+ /platform:x86 /target:winexe /unsafe";
        // If the icon is not null (as we initialize it), add the corresponding option.
        if (Icon != null)
            options += " /win32icon:\"" + Icon + "\"";

        // Set the options.
        CParams.CompilerOptions = options;
        // We don't care about warnings, we don't need them to show as errors.
        CParams.TreatWarningsAsErrors = false;

        // Add the references to the libraries we use so we can have access
        // to their namespaces.
        CParams.ReferencedAssemblies.Add("System.dll");
        CParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        CParams.ReferencedAssemblies.Add("System.Drawing.dll");
        CParams.ReferencedAssemblies.Add("System.Data.dll");
        CParams.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");

        // Check if the user specified any resource files.
        // If yes, add then to the stub's resources.
        if (Resources != null && Resources.Length > 0)
        {
            // Loop through all resource files specified in the Resources[] array.
            foreach (string res in Resources)
            {
                // Add each resource file to the compiled stub.
                CParams.EmbeddedResources.Add(res);
            }
        }

        // Dictionary variable is used to tell the compiler that we want
        // our file to be compiled for .NET v2
        Dictionary<string, string> ProviderOptions = new Dictionary<string, string>();
        ProviderOptions.Add("CompilerVersion", "v2.0");

        // Now, we compile the code and get the result back in the "Results" variable
        CompilerResults Results = new CSharpCodeProvider(ProviderOptions).CompileAssemblyFromSource(CParams, source);

        // Check if any errors occured while compiling.
        if (Results.Errors.Count > 0)
        {
            // Errors occured, notify the user.
            MessageBox.Show(string.Format("The compiler has encountered {0} errors",
                Results.Errors.Count), "Errors while compiling", MessageBoxButtons.OK,
                MessageBoxIcon.Error);

            // Now loop through all errors and show them to the user.
            foreach (CompilerError Err in Results.Errors)
            {
                MessageBox.Show(string.Format("{0}\nLine: {1} - Column: {2}\nFile: {3}", Err.ErrorText,
                    Err.Line, Err.Column, Err.FileName), "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return false;

        }
        else
        {
            // No error was found, return true.
            return true;
        }

    }
}

感谢。

1 个答案:

答案 0 :(得分:1)

虽然不是在.NET中,但是当你安装Visual Studio或安装Windows SDK时,它会自动将程序集CppCodeProvider.dll安装到包含CppCodeProvider类的GAC中。

但是我不知道部署的容易程度,您可能需要在部署时将CppCodeProvider.dll包含在您的类中,以及您发现它依赖的任何其他dll。