通过命令行编译C / C ++ / Visual C程序

问题描述 投票:0回答:1

我用C#编写了一个程序 - windows-forms从C / C ++中接收源代码的输入。我必须编译并运行,并根据他的计划显示用户输出。经过长时间的网上冲浪后,使用Microsoft的编码编译类并不成功,因为上述C语言的功能尚未实现。我创建了一个程序,打开命令行并向其发送命令,并读回输出。使用Visual Studio的内置命令行不成功,因为它在打开后几秒钟后立即关闭,即使在Internet上长时间搜索后我也找不到解决方案。

You can see here my view

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”命令,但是要加载所有环境变量。你可以在图像中看到它。

谢谢大家的帮助!!

c++ c visual-c++ command-line compilation
1个回答
0
投票

我正在使用WIN32 CreateProcess来实现相同的结果。

0)总是阅读文档:https://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx

1)winsdkver.h是一个包含在Windows Sdk中的文件。你有WSDK吗?

2)你需要暂停cmd执行吗?使用'pause'命令在cl.exe之后添加一行。

3)在cl.exe参数字符串中添加一些带有/ I的include dir以包含其他标头。

4)您还可以创建批处理文件:'cl.exe%*> output.txt'这允许您将参数传递给cl.exe,然后将结果打印在文本文件中

© www.soinside.com 2019 - 2024. All rights reserved.