Issue
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:
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:
'-DHOST=(char*)"https://google.fr/"'
I do not understand how to format correctly
Solution
The QProcess::start
function has several overloads. 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);
Answered By - pschill