c++ - 在 QT5 中使用 QProcess 运行 gcc

标签 c++ gcc qt5

我需要从第一个程序编译源代码。 我需要运行这个:

gcc -o finalOutput sources/main.cpp sources/config.h -lcurl '-DHOST=(char*)"https://google.fr/"'

我用的是QT5,下面是我测试的:

QProcess *proc;  
proc = new QProcess();  
proc->start("gcc -o finalOutput sources/main.cpp sources/config.h -lcurl '-DHOST=(char*)"https://google.fr/"'"); // start program
ui->lblReturn->setText("ok");

问题出在gcc命令的语法上,这部分:
'-DHOST=(char*)"https://google.fr/"'

QT5

我不明白如何正确格式化

最佳答案

QProcess::start 函数有几个重载。第一版

QProcess::start(const QString& command, OpenMode mode=ReadWrite);

对包含引号字符的参数有奇怪的行为。引用文档:

Literal quotes in the command string are represented by triple quotes.

这就是为什么我通常推荐

QProcess::start(const QString& program, const QStringList& arguments, OpenMode mode=ReadWrite);

过载。使用这个,命令

gcc -o finalOutput sources/main.cpp sources/config.h -lcurl '-DHOST=(char*)"https://google.fr/"'

可以用下面的代码执行:

QStringList args = QStringList()
    << "-o"
    << "finalOutput"
    << "sources/main.cpp"
    << "sources/config.h"
    << "-lcurl"
    << "-DHOST=(char*)\"https://google.fr/\"";
QProcess *proc = new QProcess();
proc->start("gcc", args);

关于c++ - 在 QT5 中使用 QProcess 运行 gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50436030/

相关文章:

assembly - 如何在 GCC 中的函数调用中不使用堆栈?

c - 输入使用 gets 不起作用,但使用 scanf

qt5 - qmake.conf 和父目录中的项目文件

c++ - 从 QML 中的 C++ 类接收带有参数的信号

c++ - 分配大内存的最佳方法

c++ - 在 C++ 中实现正则表达式

c# - 从 C++ 应用程序访问 C# 用户设置

c++ - 将字符串映射到 int CPP - 输出在执行期间挂起

c++ - gcc 中的 -O2 和 -fPIC 选项

c++ - Qt 的 nativeEvent() 从未被调用