简体   繁体   English

在QT5中使用QProcess运行gcc

[英]Run gcc with QProcess in QT5

I need to compile a source from a first program. 我需要从第一个程序编译源代码。 I need to run this: 我需要运行:

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

I use QT5, here is what I tested: 我使用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");

The problem comes from the syntax of the gcc command, this part: 问题来自gcc命令的语法,这一部分:
'-DHOST=(char*)"https://google.fr/"'

QT5

I do not understand how to format correctly 我不明白如何正确格式化

The QProcess::start function has several overloads. QProcess::start函数具有多个重载。 The first version 第一个版本

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

has a strange behavior with arguments that contain quote characters. 具有包含引号字符的参数的行为很奇怪。 To cite the documentation: 引用文档:

Literal quotes in the command string are represented by triple quotes. 命令字符串中的文字引号由三引号表示。

That's why I usually recommend the 这就是为什么我通常推荐

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

overload. 超载。 Using this, the command 使用此命令

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

can be executed with the following code: 可以使用以下代码执行:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM