1

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

enter image description here

Waqas Shabbir
  • 755
  • 1
  • 14
  • 34
Muhammad Waqas
  • 85
  • 4
  • 15
  • The error message tells you that you have not set the include path for the compiler. Related: https://stackoverflow.com/questions/931652/fatal-error-c1034-windows-h-no-include-path-set – drescherjm Jan 31 '19 at 14:40
  • 1
    This won't solve the problem at hand, but you might want to consider an alternative to hardcoding the paths if possible. – EJoshuaS - Stand with Ukraine Jan 31 '19 at 14:45
  • You could set the environment variables with: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.environmentvariables?view=netframework-4.7.2 or add additional arguments to to your `start.Arguments =` for the include and linker paths. – drescherjm Jan 31 '19 at 14:50

1 Answers1

1

try to first run

 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat

For executing both scripts at the same time, you can use below approach

Process.Start("cmd.exe", "/c \"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\Tools\VsDevCmd.bat\"\n\"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe\" /EHsc D:\test\test.cpp /out:D:\test\test.exe");

For more information: Check This

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • How can I run it from the C# code, can you please show some code. – Muhammad Waqas Jan 31 '19 at 14:47
  • Thanks, but the code you have shared is not working, I am getting the following error. "'C:\Program' is not recognized as an internal or external command, operable program or batch file." @Simonare – Muhammad Waqas Feb 01 '19 at 02:36