0

I am interested in developing C++ IDE, but for ease and better UI, I want to develop the IDE using C#. But I am stuck with a problem that I cant find how to compile the C++ code using C# and build the ".exe" of C++ program that was compiled using my IDE. Can anyone please suggest me if their is any API or Library that can Build the ".exe" of any C++ code that I give input.

I am able to compile and build C# code, but can anyone please help me if there is a way that i can integrate the c++ compiler feature in my application. What i need is something similar to CSharpCodeProvider but for compiling C++ code.

What i m doing for C# compiling is in the code below.

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;
        }

    }
}

Thanks.

13

1 Answer 1

1

While not in .NET it self when you install Visual Studio or install the Windows SDK it will install the assembly CppCodeProvider.dll in to the GAC which includes the CppCodeProvider class.

However I don't know how easily it will be to deploy, you may need to include the CppCodeProvider.dll in with your class when you deploy it and any other dll's you find that it depends on.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks alot :) But i still want to know that the ".exe" of the compiled C++ code will require .net to run or not?
No, it would not require .net, it may or my not require the C++ Runtime to be installed depending on your compliation options, but it would not require .net.
I tried alot to use CppCodeProvider, but cant do it properly because documentation is not provided properly any where. Can you please guide me Scott. I tried few implementation, but cant achieve success. stackoverflow.com/questions/23553159/…
I have never used it, I have no guidance on this issue.
well ok, np :) Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.