I want to execute a sub-process in C++. I need it to work on Windows and Linux. Is there such a function in Boost? What is the standard way of doing it?
问题:
回答1:
Poco and ACE have Process classes that do what you want. See Foundation->Processes->Process in Poco; Process.h/Process.cpp for Ace. I wouldn't be surprised if QT has something similar.
As for how to do it, basically you wrap the OS dependencies and bury the details. Poco and Ace offer contrasting common methods. Poco tends to handle things by writing implementation objects (xxx_impl) for each platform with the proper one getting pulled in depending on the OS. ACE seems to #ifdef the code, sometimes to the point of madness, though in fairness it has been a long time since I looked at that code.
回答2:
There is the not-yet-approved Boost.Process library. I never tried it, but it may do the job for you.
I've successfully used Poco's Process API though.
回答3:
What level of control do you want? The standard includes system()
, which can execute a sub-process. If you want to control standard input or standard output, you can use popen
(though MS usually calls it _popen
). You only really need to look elsewhere if you want something more elaborate than that.
回答4:
ANSI C89 system()
exists on both platforms. Obviously what that process does depends on whether it works portably between platforms. But you certainly don't need boost to do it.