在 C++11 中缺少对执行执行的"missing sentinel"警告

Missing "missing sentinel" warning for exec in C++11

本文关键字:执行 sentinel 警告 missing C++11      更新时间:2023-10-16

如果您忘记在对exec(3)函数之一的调用结束时包含NULL sentinel,GCC将向您发出有用的警告:

#include <unistd.h>
int main(int argc, char **argv)
{
  execlp("test", "test", "arg1");
}

GCC 4.8的示例编译器输出:

$ g++ test.cc -Wformat
test.cc: In function ‘int main(int, char**)’:
test.cc:4:32: warning: missing sentinel in function call [-Wformat=]
   execlp("test", "test", "arg1");
                            ^
$

但是,如果您在C++11模式下编译,则不会打印诊断:

$ g++ test.cc -std=c++11 -Wformat
$

为什么C++11中没有此警告?有办法把它拿回来吗?

execlp不是标准的C函数。为了让编译器将其识别为"标准"函数,它知道参数应该是什么样子,需要-std=gnu++11而不是-std=c++11。请注意,默认值为-std=gnu++98。Glibc可以通过在execlp的声明中指定sentinel属性来改善这种情况。