更新时间:2023-11-22 17:31:58
您所要求的实际上很简单:
What you asked for is actually quite easy:
{
pid_t pid = fork();
if (pid == 0)
{
const char* error_reporter_path = "./error_reporter";
execl(error_reporter_path,
error_reporter_path,
(char *) 0);
_exit(127);
}
else
return pid != -1;
}
但是它不能满足您的要求. 当前工作目录与包含当前可执行文件的目录不一定是同一件事-实际上,在几乎所有情况下都不会.
but it doesn't do what you want. The current working directory is not necessarily the same thing as the directory containing the current executable -- in fact, under almost all circumstances, it won't be.
我建议您做的是将error_reporter_path
设置为全局变量,并使用选项2"代码在main
的开始处对其进行初始化
What I would recommend you do is make error_reporter_path
a global variable, and initialize it at the very beginning of main
, using your "option 2" code
QString("%1/%2")
.arg(QApplication::applicationDirPath())
.arg("error_reporter").toLatin1().constData();
QString
对象(不仅是它的constData
)然后必须在程序的生存期内生存,但这不应该成为问题.请注意,您应该转换为UTF-8,而不是Latin1(我猜QString
使用宽字符?)
The QString
object (not just its constData
) then has to live for the lifetime of the program, but that shouldn't be a problem. Note that you should be converting to UTF-8, not Latin1 (I guess QString
uses wide characters?)