c - type checking across source files -
i spent many hours debugging problem turned out caused 2 source files including 2 header files in different order. 1 of headers defined _file_offset_bits 64, , other header file included <sys/types.h>, defined off_t either 32 or 64 bits long, depending on setting of _file_offset_bits. i've included below short example of situation. on x86_32 linux (both debian unstable , centos 4.8).
neither gcc -wall main.c other.c, nor solaris 9 lint, nor splint detects situation.
does know of software tool can detect situation?
main.c
#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 %d\n", sizeof(off_t)); return 0; } other.c
#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 %s\n", p->s); } printf("sizeof(off_t) in other.c %d\n", sizeof(off_t)); } header.h
struct foo { off_t offset; const char * s; }; extern void showproc(const struct foo *); program output
null pointer reference sizeof(off_t) in other.c 4 sizeof(off_t) in main.c 8
i recommend putting defines modify headers in makefile instead of in code. otherwise can't sure compilation units have 1 definition or other, you've experienced.
sometimes modifying headers macros intended behavior (e.g. using headers templates) , isn't (like in case), hard produce meaningful warnings tool, think.
Comments
Post a Comment