如何与 C++ 中的可执行文件交互
我有一个可执行文件,需要在 C++ 中运行一些测试 - 测试将在所有 Windows、Linux 和 Mac 操作系统上进行。
我希望得到以下方面的意见:
- 我将如何与我的代码中先前构建的可执行文件进行交互?我可以使用某种命令功能吗?另外,由于我认为命令在操作系统之间会发生变化,因此我需要一些指导来弄清楚如何为所有三个操作系统构建结构。
编辑 - Interface = 我需要能够使用 C++ 代码中的命令行参数运行可执行文件。
- 从命令行调用时,可执行文件还会将一些文本输出到控制台上 - 我如何才能获取该输出流(我需要记录这些输出值作为测试的一部分)。
请随时向我询问后续问题。
干杯!
I have an executable that I need to run some tests on in C++ - and the testing is going to take place on all of Windows, Linux and Mac OSes.
I was hoping for input on:
- How would I interface with the previously built executable from my code? Is there some kind of command functionality that I can use? Also, since I think the commands change between OSes, I'd need some guidance in figuring out how I could structure for all three OSes.
EDIT - Interface = I need to be able to run the executable with a command line argument from my C++ code.
- The executable when called from the commandline also ouputs some text onto a console - how would I be able to grab that ouput stream (I'd need to record those outputted values as part of my tests).
Feel free to ask me follow up questios.
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 qt 开发代码,您会发现 QProcess 将允许您以与平台无关的方式生成命令行程序。
本质上:
然后您可以使用各种函数调用(例如 readAllStandardOutput ())从进程中读取数据,并使用 QProcess::write(QString) 写入进程的输入。
或者,如果您更喜欢 Boost 而不是 Qt,Boost。 Process 还可以让您启动进程。我承认我不太喜欢这种语法...
然后您可以使用流运算符 << 来处理子进程“c”和>>阅读和写作。
If you use qt to develop your code, you'll find QProcess will allow you to spawn a command line program in a platform-agnostic way.
Essentially:
You can then read from the process with various function calls such as readAllStandardOutput (), and write to the input of the process with QProcess::write(QString).
Alternatively, if you prefer Boost to Qt, Boost.Process will also let you launch processes. I confess I don't like the syntax as much...
You can then work with your subprocess 'c' by using stream operators << and >> to read and write.
所有这些操作系统都支持某种形式的“子进程”调用技术,测试人员可以在其中创建一个新的子进程并在那里执行被测试的代码。您不仅可以传递命令行,而且还有机会将管道附加到子进程的标准输入和输出流。
不幸的是,没有标准的 C++ API 来创建子进程。您必须为每个操作系统找到合适的 API。例如,在 Windows 中,您可以使用 CreateProcess 函数: MSDN:创建进程 (Windows)。
另请参阅Stackoverflow:如何在 C 中生成另一个进程?
All those OSes support some form of "subprocess" calling technique, where your tester creates a new child process and executes the code under test there. You get to not only pass a command line, but also have the opportunity to attach pipes to the child process' standard input and output streams.
Unfortunately, there is no standard C++ API to create child processes. You'll have to find the appropriate API for each OS. For example, in Windows you could use the CreateProcess function: MSDN: Creating Processes (Windows).
See also Stackoverflow: How do you spawn another process in C?
据我了解,您想要:
像 QProcess 这样的库可以生成进程,但是,我建议在 Windows 和 MacOS/Linux 上手动执行此操作,因为在这种情况下使用 QProcess 可能有点过分了。
对于 MacOS/Linux,我会执行以下操作:
pipe
在父进程中。将管道的读取端设置为父级中的新文件描述符。分叉
。execvp
并传递目标可执行文件以及您想要为其提供的参数。wait
等待子进程 (选修的)。As I understand, you want to:
Libraries such as QProcess can spawn processes, however, I would recommend doing it by hand for both Windows and MacOS/Linux as using QProcess for this case is probably overkill.
For MacOS/Linux, here's what I would do:
pipe
in the parent process. Set the read end of the pipe to a new file descriptor in the parent.fork
.execvp
in the newly created child process and pass the target executable along with what arguments you want to give it.wait
for the child (optional).首先,您是否可能只是想让原始代码可重用?在这种情况下,您可以将其构建为库并将其链接到您的新应用程序中。
如果您确实想与另一个可执行文件进行通信,那么您可能需要将其作为主应用程序的子进程启动。我会推荐 Process 类.org/" rel="nofollow noreferrer">Poco C++ 库。
First of all, is it possible that you simply need to want to make your original code reusable? In that case you can build it as library and link it in your new application.
If you really want to communicate with another executable then you can need start it as a subprocess of the main application. I would recommend the Process class of the Poco C++ libraries.
看起来像是 popen() 的工作,可在 Linux 上使用,Windows 和 OS X
Looks like a job for popen(), available on Linux, Windows, and OS X
听起来您只计划在可执行级别进行功能测试。这还不够。如果您打算进行彻底的测试,您还应该编写单元测试。为此,有一些优秀的框架。对于 C++,我(到目前为止)最喜欢的一个是 BOOST::Testing。
如果您控制源代码,除了从外部进程启动 exe 之外,还有一些常见的功能测试技巧:嵌入功能测试。您只需向执行测试的程序添加一个选项即可。这很酷,因为测试嵌入在代码中,并且可以在任何执行环境中轻松启动自动检查。
这意味着在测试环境中,当您使用一些测试专用参数调用程序时,没有什么可以阻止您完全执行并重定向 stdout 的内容,甚至从程序内检查测试结果。与从外部启动器调用然后分析启动器的结果相比,这将使整个测试变得更加容易。
Sounds like you are only planning to do functional testing at the executable level. That is not enough. If you plane to do thorough testing, you should also write unit tests. For that there is some excellent frameworks. My prefered one (by far) for C++ is BOOST::Testing.
If you control source code there is also common tricks for functional testing beside launching exe from an external process : embed functional tests. You just add an option to your program that execute tests. This is cool because tests are embedded in code and autocheck can easily be launched in any execution environment.
That means that in the test environment, as you call your program with some test dedicated arguments, nothing keeps you from going the full way and redirect the content of stdout and even check the tests results from within the program. It will make the whole testing much easier than calling from an external launcher, then analysing the results from than launcher.