2

当我执行 pclint 时,我发现包含头文件在 c / c++ 文件中定义的顺序存在一些问题。

说包含顺序是,

#include <sys/timerfd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdarg.h>                                       
#include <string.h>

当我执行 pclint 时,它会给出错误,文件未声明等。

后来我将包含的顺序更改为

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdarg.h>                                       
#include <string.h>
#include <sys/timerfd.h>

我可以看到很多错误都消失了。我无法弄清楚为什么会出现这种行为。我正在为 C/C++ (NT) Vers 使用 PC-lint。8.00 瓦。

我已将包含路径标记为 +libdir(D:\timesys\nitrogen6x\toolchain\include)

谢谢你,布里杰什

4

1 回答 1

1

据说,包含头文件确实有点重要,尽管这种情况很少见。一些包含文件使用类型、枚举或仅在另一个包含文件中定义的其他内容。

例如,在 Linux 上,某些函数需要包含多个标头。如果您以错误的顺序包含这些标头,则某些使用这些标头的程序会失败。有点像最后的链接阶段。您必须以正确的顺序设置库,否则您可能会得到未解决的依赖关系。

如果我找到一个例子,我会在这里发布。

编辑:找到一个例子。Qt。Qt 拥有最复杂的标题集。例如,如果你在 QtOpenGL.h 之前包含 opengl.h,它会给你一个编译错误,因为在 Qt 头文件中它会检查是否包含 opengl。出于某种原因,QtOpenGL.h 必须排在第一位。

于 2014-02-26T17:29:10.073 回答