1

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.

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.

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

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!";
    return 0;
}

Error:

 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: 

Thanks :)

5
  • That's not even valid C++. Commented May 8, 2014 at 21:52
  • @Moo-Juice , I have been writing programs using the same C++ Syntax for quite few time. I will be grateful to you if you can correct C++ Syntax. Maybe I can try with that :) Thanks Commented May 8, 2014 at 21:54
  • Your main function needs to return an int at the very least (given that you're going return 0). This is probably not the source of your problem, however - just an observation. Commented May 8, 2014 at 21:56
  • @Moo-Juice That's my bad. Thanks for the correction, but I will be thankful if you can help me with my Problem :) Commented May 8, 2014 at 21:59
  • 2
    No, you're on the wrong track. CppCodeProvider only implements half of what code providers normally implement. You can get a code generator but you can't get it built. Wrong code generator btw, it generates C++/CLI, not C++. Building C++ is a painful affair, stuck with a build model from the 1970s and several very non-trivial changes in the way it is done in the past 4 years. A major revision in 2010, switching from a legacy build system to MSBuild. Again in 2013 with a heavy internal MSBuild overhaul. Using the Process class with stdout redirection is the best way. Commented May 8, 2014 at 23:20

2 Answers 2

2

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.

The documentation is here:

It says:

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.

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.

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

2 Comments

Thanks alot. But can you please also mention that how can I compile c++ code. Can you please give me some example?
After a long digging, I found that Compilation of CPP is not supported by Microsoft Currently. You can use any C++ Compiler that supports command line (gcc, Mingw etc.) to compile your c++ code
1

After a long digging, I found that Compilation of CPP is not supported by Microsoft Currently. You can use any C++ Compiler that supports command line (gcc, Mingw etc.) to compile your c++ code.

The strange thing is when you check the following code it returns true, but the method is not implemented by Microsoft.

CodeDomProvider.IsDefinedLanguage("Cpp")

If anyone find that I am wrong, or find any better way to do this, please let me know. Thanks :)

1 Comment

Compiling is not the only task for CodeDOM, another big thing is emitting source code files in the language for various design-time features (resource generator, WinForms designer, etc). So the language might be partially supported for that purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.