簡體   English   中英

通過命令行編譯C / C ++ / Visual C程序

[英]Compiling C/C++/Visual C program by Command Line

我用C#-Windows-forms編寫了一個程序,該程序從C / C ++的源代碼中接收輸入,然后我必須對其進行編譯和運行,並根據他的計划顯示用戶輸出。 經過長時間的互聯網瀏覽,Microsoft的codedom編譯類的使用不成功,因為上述C語言功能尚未實現。 我創建了一個程序,該程序打開命令行並向其發送命令,然后將輸出讀回。 使用Visual Studio的內置命令行並不成功,因為它在打開后幾秒鍾后立即關閉,即使在Internet上長時間搜索后,我也找不到解決方案。

你可以在這里看到我的看法

And this is the code:

      string vcvars32 = @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat";
        string vsdevcmd = @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat";
        string WorkingDirectory = @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin";
        string sourceFilePath = @"E:\C_Sources\sample.c";

 private void button1_Click(object sender, EventArgs e)
        {
            saveToFile(textBox1.Text);//Save the code as c file.
            textBox3.Text = runCmd();
        }

  string runCmd()
    {
        Process p = new Process();
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = @"cmd.exe";
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;
        info.RedirectStandardOutput = true;
        info.CreateNoWindow = true;
        p.StartInfo = info;
        p.Start();

        using (StreamWriter sw = p.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine(vsdevcmd );
                sw.WriteLine("cd " + WorkingDirectory);
                sw.WriteLine(vcvars32);
                sw.WriteLine();
                sw.Write("cl.exe ");

                sw.WriteLine(sourceFilePath);
            }
        }
        using (StreamReader sr = p.StandardOutput)
        {
            if (sr.BaseStream.CanRead)
            {
                toolStripStatusLabel1.Text = "Parse completed";

                return sr.ReadToEnd();
            }
        }

        p.WaitForExit();
        toolStripStatusLabel1.Text = "cannot read output";
        return "";
    }

某些方法可行,但出現如下錯誤:

致命錯誤C1034:winsdkver.h:否包含路徑集

雖然我調用“ vcvars32.bat”命令,以加載所有環境變量。 您可以在圖像中看到它。

謝謝大家的幫助!

我正在使用WIN32 CreateProcess來達到相同的結果。

0)始終閱讀文檔: https : //msdn.microsoft.com/zh-cn/library/fwkeyyhe.aspx

1)winsdkver.h是Windows Sdk中包含的文件。您有WSDK嗎?

2)您需要暫停cmd執行嗎? 使用“ pause”命令在cl.exe之后添加一行。

3)在cl.exe參數字符串中添加一些帶有/ I的包含目錄,以包含其他標頭。

4)您還可以創建一個批處理文件:'cl.exe%*> output.txt',這允許您將參數傳遞給cl.exe,然后將結果打印到文本文件中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM