How to compile C/C++ file dynamically from C#

  • Thread starter Thread starter waqasm78
  • Start date Start date
W

waqasm78

Guest
I want to compile C/C++ file dynamically from C# code.

For example I have a c++ file (test.cpp)

#include <iostream>
using namespace std;
void main()
{
cout << "Hello, world, from Visual C++!" << endl;
}

Here is the C# code where i want to compile the *.cpp file.

class Program
{
static void Main(string[] args)
{
var start = new ProcessStartInfo();

start.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe";
start.Arguments = @" /EHsc D:\test\test.cpp /out:D:\test\test.exe";

start.UseShellExecute = false;
start.RedirectStandardOutput = true;

string StandardOutput = "";

using (var process = Process.Start(start))
{
// Could be useful to eventually track error
using (var reader = process?.StandardOutput)
{
StandardOutput += reader?.ReadToEnd();
}
}

Console.WriteLine(StandardOutput);
}
}

When the code is executed it gives the following error.

Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27026.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved.

cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release test.cpp D:\test\test.cpp(1): fatal error C1034: iostream: no include path set

7R5pn.png

Continue reading...
 
Back
Top