我花了很多时间调试一个问题,该问题原来是由两个源文件引起的,其中包括两个以不同顺序排列的头文件。其中一个头文件定义_FILE_OFFSET_BITS为 64,另一个头文件包括<sys/types.h>,定义off_t为 32 位或 64 位长,具体取决于_FILE_OFFSET_BITS. 我在下面提供了这种情况的简短示例。这是在 x86_32 Linux(Debian 不稳定版和 CentOS 4.8)上。
无论是gcc -Wall main.c other.c,还是的Solaris 9皮棉,也没有夹板检测到这种情况。
有谁知道可以检测这种情况的软件工具?
#define _FILE_OFFSET_BITS 64
#include <sys/types.h>
#include <stdio.h>
#include "header.h"
int
main(int argc, char **argv) {
struct foo bar = {(off_t) 0, "foo"};
showproc(&bar);
printf("sizeof(off_t) in main.c is %d\n", sizeof(off_t));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <sys/types.h>
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include "header.h"
void
showproc(const struct foo *p)
{
if (p->offset == 0) {
if (p->s == NULL)
puts("NULL pointer reference");
else
printf("Structure value is %s\n", p->s);
}
printf("sizeof(off_t) in other.c is %d\n", sizeof(off_t));
}
Run Code Online (Sandbox Code Playgroud)
struct foo {
off_t offset;
const char * s;
};
extern void showproc(const struct foo *);
Run Code Online (Sandbox Code Playgroud)
NULL pointer reference
sizeof(off_t) in other.c is 4
sizeof(off_t) in main.c is 8
Run Code Online (Sandbox Code Playgroud)