none
How to compile C/C++ file dynamically from C# RRS feed

  • Question

  • 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

    Friday, February 1, 2019 3:29 AM

Answers

  • Try this:

    start.FileName = @"D:\Test\b.bat";

    start.Arguments = null;

     

    where 'b.bat' is a temporary file that is generated by your program (try it manually too), containing two lines:

     

    call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat"
    "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 /link /out: D:\test\test.exe
    It also includes the "/link" option.

    • Marked as answer by waqasm78 Friday, February 1, 2019 10:02 AM
    Friday, February 1, 2019 6:20 AM
  • Try specifying the /Fo option (https://docs.microsoft.com/en-us/cpp/build/reference/fo-object-file-name?view=vs-2017) before /link:

       /FoD:\Test\

    The path must end with ‘\’ or ‘/’.

    • Marked as answer by waqasm78 Friday, February 1, 2019 12:14 PM
    Friday, February 1, 2019 10:35 AM

All replies

  • 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

    Friday, February 1, 2019 3:22 AM
  • Hi waqasm78,

    Welcome to the MSDN forum.

    According to the description, this issue is about the C# development and our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, February 1, 2019 6:18 AM
  • Try this:

    start.FileName = @"D:\Test\b.bat";

    start.Arguments = null;

     

    where 'b.bat' is a temporary file that is generated by your program (try it manually too), containing two lines:

     

    call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat"
    "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 /link /out: D:\test\test.exe
    It also includes the "/link" option.

    • Marked as answer by waqasm78 Friday, February 1, 2019 10:02 AM
    Friday, February 1, 2019 6:20 AM
  • thank you so much, it works :)

    Now I am facing another problem when executing this asp.net MVC. Actually it creates test.obj file in IIS Express folder which throws an error.

    fatal error C1083: Cannot open compiler generated file: 'C:\Program Files (x86)\IIS Express\test.obj': Permission denied.

    Is there a way to specify the obj file path in batch file as we have specified cpp and exe file paths. 

    • Marked as answer by waqasm78 Friday, February 1, 2019 12:14 PM
    • Unmarked as answer by waqasm78 Friday, February 1, 2019 12:14 PM
    Friday, February 1, 2019 9:45 AM
  • Try specifying the /Fo option (https://docs.microsoft.com/en-us/cpp/build/reference/fo-object-file-name?view=vs-2017) before /link:

       /FoD:\Test\

    The path must end with ‘\’ or ‘/’.

    • Marked as answer by waqasm78 Friday, February 1, 2019 12:14 PM
    Friday, February 1, 2019 10:35 AM
  • Thanks you so much :) it works
    Friday, February 1, 2019 12:15 PM