QT5中使用QProcess调用gcc编译的宏定义参数格式问题
Fixing the QProcess GCC Argument Issue
The problem here is that when you pass the entire GCC command as a single string to QProcess::start(), Qt’s argument parsing doesn’t handle the quoted -DHOST parameter the same way a shell does. The correct approach is to split your command into individual arguments using a QStringList—this avoids quoting confusion because each list item is treated as a separate, intact argument.
Here’s the corrected code:
QProcess *proc = new QProcess(); QStringList args; args << "gcc" << "-o" << "finalOutput" << "sources/main.cpp" << "sources/config.h" << "-lcurl" << "-DHOST=(char*)\"https://google.fr/\""; // No outer single quotes needed here proc->start("gcc", args); // Pass executable name and arguments separately ui->lblReturn->setText("ok");
Why this works:
- Each element in the
QStringListis passed directly to GCC as a single argument. You don’t need the outer single quotes used in shell commands because Qt handles argument separation natively. - The double quotes around the URL are escaped with a backslash (
\") to be properly interpreted in the C++ string literal.
Bonus Best Practices:
- Always use the
QProcess::start(const QString &program, const QStringList &arguments)overload instead of passing the full command as a string—it’s far more reliable for complex arguments with spaces, quotes, or special characters. - For debugging, add checks like
proc->waitForStarted()andproc->error()to catch any issues with process startup or execution.
内容的提问来源于stack exchange,提问作者atmon3r
相关产品推荐
相关产品推荐

