tags:

views:

309

answers:

7

For business reasons, I want to create a C# application that would take a C++ file / snippet as input, compile it (probably invoking a C++ compiler under the hood) and output compilation results.

Do you know how this could be done?

Thanks in advance.

+4  A: 

Using CL.exe

aJ
A: 

You can shell out to any number of command-line C++ compilers (like gcc) using Process.Start.

Jekke
+1  A: 

Most compilers support command-line parameters. You just need to build the right command and execute it through the shell like advised here. che If you want to not just build a single file, but a whole .vcproj file - check the command line parameters for devenv.exe. If I remember correct it is:

devenv.exe /build my.vcproj
KIV
+2  A: 

Look in to the Process class.

It provides all of the functionality required to start an external application, including a compiler.

Now, depending on the compiler you choose, you will need to specify the start arguments of the process carefully in order to compile in a predictable way.

John Gietzen
A: 

You could do this by calling any C++ compiler on the command line. I'm sure the compilation results can be redirected so that you can grab it after the compile finished.

m0rb
A: 

If you are trying to achieve something like SnippetCompiler for C++, you might want to look at one of the C/C++ scripting languages. I have always liked CInt.

R Ubben
A: 

Sure its possible, it is actually common practice nmake and other make-like utilities call the compiler all the time.

OTOH if you are thinking of deploying this solution to a customer you may be in for a bit of rough ride cause of all the details like settings, location where files are, how to react on the result, license, which c++ compiler to deploy etc.

Another option which is much simpler (IMHO) is instead of C++ to invoke the C# compiler directly from your C# program for compiling a C# snippet. Files can then stay in memory and you don't need to save any files before you compile. There are numerous examples on the web that show how to do that. Another advantage with this solution is that you already have the C# compiler there so there is no need to install a C++ compiler as well.

Anders K.