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 :)
mainfunction needs to return anintat the very least (given that you're goingreturn 0). This is probably not the source of your problem, however - just an observation.