从C#代码编译c和cpp文件 [英] compiling c and cpp files from C# code

查看:226
本文介绍了从C#代码编译c和cpp文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从命令提示符下编译cpp和c文件.但是我无法区分错误和警告.当我遇到错误时,我需要中止该过程,并在收到警告时要继续.

[实际要求:-当我们单击按钮时,应运行可编译数百个c cc cpp文件的代码]

i am using following code to compile cpp and c files from command prompt. but i am unable to differentiate the errors and warnings . i need abort the process when i get an error and want to continue when i get a warning.

[Actual requirement:- when we click on a button code should be run which can compile hundreds of c anc cpp files ]

public string ExecuteCommandSync(string command,string file)
       {
               MessageBox.Show(command);
               string strError=string.Empty;
               // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
               // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.

               System.Diagnostics.ProcessStartInfo procStartInfo = new
                   System.Diagnostics.ProcessStartInfo("cmd", "/c" + command);
               // The following commands are needed to redirect the standard output.
               //This means that it will be redirected to the Process.StandardOutput StreamReader.
               procStartInfo.RedirectStandardOutput = true;
               procStartInfo.RedirectStandardError = true;
               procStartInfo.UseShellExecute = false;
               // Do not create the black window.
               procStartInfo.CreateNoWindow = true;
               // Now we create a process, assign its ProcessStartInfo and start it
               System.Diagnostics.Process proc = new System.Diagnostics.Process();
               proc.StartInfo = procStartInfo;
               proc.Start();

               // Get the output into a string
               string result = proc.StandardOutput.ReadToEnd();
               //MessageBox.Show("warning"+result);
               //if (result == "")
               //{
               //    result += command + "success";
               //    ce_obj.statusMsg(result);
               //}
               //strError += "standardoutput----->"+proc.StandardOutput.ReadToEnd()+"<\r\n";
               strError = proc.StandardError.ReadToEnd();
               //MessageBox.Show("error"+strError);
               return result;

       }






请建议我该代码是否正确.
在这段代码中,我无法在编译时区分错误和警告.


请向我建议另一种编译方式.






please suggest me whether is this code correct or not.
in this code i am unable to differentiate in between an error and warning while ccompiling.


please suggest me an alternate way of compiling.

推荐答案

不幸的是,除了问题的文本之外,错误结束警告通常没有任何区别.您将需要逐行阅读或将流解析为几行,并通过一行中某个位置的错误"或警告"之类的关键字来识别问题报告的类型.为此,您应该分析特定编译器/链接器的输出以了解其确切的字符串格式.

这种区分的其他方法可能是错误代码.您可以查看特定编译器/链接器的文档.

您遇到的一个问题是:使用"cmd""/c".您永远不需要它.您应该立即使用应用程序的命令行:可执行文件的完整路径名和所需的命令行.

实际上,通常需要在单独的线程中运行代码.由于C或C ++编译器的工作速度非常慢,因此您甚至可以并行读取数据,并在读到最后之前即时通知您的UI线程.请查看我过去有关相关主题的答案:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
Unfortunately, the errors end warnings are usually not differentiated in any way except the text of the issue. You would need to read line by line or parse the stream into lines and recognize the type of the issue report by a keyword like "Error" or "Warning" at a certain position in a line. For this purpose, you should analyze the output from a particular compiler/linker to understand its exact string format.

Some other method of such differentiation could be an error code. You can see documentation of a particular compiler/linker.

One problem you have is this: using "cmd" "/c". You never really need it. You should use immediately the command line of your application: full path name of executable file and required command line.

In practice, you usually need to run your code in a separate thread. As C or C++ compilers can work pretty slowly, you could even read the data in parallel and notify your, say, UI thread on the fly, before you read to the end. Please see my past answers on related topics:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^],
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于从C#代码编译c和cpp文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆