下面是 C++ include guard 的拼写错误。两者都应读取 _MYFILE_H_
。
#ifndef _MYFILE_H_
#define _MYFLIE_H_
您如何建议使用 GNU coreutils(例如 grep、awk)在一堆头文件中搜索这样的拼写错误?
最佳答案
你可以使用 awk
:
{
if ($1 == "#ifndef") { was_ifdef = 1; def = $2 }
else if ($1 == "#define" && was_ifdef)
{
if ($2 != def) printf("Mismatch in %s: expected %s found %s\n", FILENAME, def, $2);
}
else was_ifdef = 0;
}
可能有更聪明的方法来做到这一点,但这(对我来说)非常清楚和容易。
注意:如果文件包含类似
的内容,这将显示“误报”#ifndef FOO_DEFINED
typedef int foo;
#define FOO_DEFINED
#endif
关于c++ - 使用 gnu coreutils 在 C++ 中查找 include guard 拼写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15011869/