阅读量:109
在C++中,你可以使用第三方库来执行CMD命令
-
首先,确保已经安装了Boost库。如果没有安装,请访问Boost官网下载并安装。
-
在你的C++项目中,包含所需的头文件:
#include
#include
- 使用
boost::process::system()函数执行CMD命令。例如,要执行ipconfig命令,你可以这样做:
int main() {
try {
int result = boost::process::system("ipconfig");
std::cout << "Command executed with exit code: "<< result class="hljs-built_in">catch (const std::exception& e) {
std::cerr << "Error executing command: " << e class="hljs-built_in">what()<< std class="hljs-keyword">return 0;
}
- 编译并运行你的程序。确保链接Boost库。例如,如果你使用g++编译器,可以这样做:
g++ main.cpp -o main -lboost_system
注意:-lboost_system参数用于链接Boost系统库,这是Boost进程库所需的。
现在,当你运行程序时,它将执行ipconfig命令并显示结果。你可以根据需要替换为其他CMD命令。