一般我们都知道include<>是系统头文件(即编译器默认的那些头文件),include""是引用自定义的一些头文件,一般放于当前目录下。
今天在VC6下写一个小程序,自己定义了一些结构体,也从VC6自带的那些头文件中复制了几个结构体(想使程序能轻易地移植到其它平台上):
typedef struct tagBMP256{
BYTE *pData;
LONG pWidth;
LONG pHeight;
}BMP256;
typedef struct tagBMPFILEHEADER {//原来名字是tagBITMAPFILEHEADER,不改的话会出现redefinition错误
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BMPFILEHEADER;
……
声明了两个函数
void read_bmp256(FILE *fp, BMP256 *img);
void write_bmp256(FILE *fp, BMP256 *img);
由于用了WORD,DWORD,LONG,所以就在文件头部引用了windef.h,结果因此带来了相当大的麻烦。
随便测试了一下,一堆错误
--------------------Configuration: PeronaMalik - Win32 Debug--------------------
Compiling...
anisodiff2D.c
d:/program files/microsoft visual studio/vc98/include/winnt.h(3143) : error C2061: syntax error : identifier 'PCONTEXT'
d:/program files/microsoft visual studio/vc98/include/winnt.h(3144) : error C2059: syntax error : '}'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2143: syntax error : missing ')' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2143: syntax error : missing '{' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2371: 'BMP256' : redefinition; different basic types
g:/vc_projects/peronamalik/src/imageformat.h(55) : see declaration of 'BMP256'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2143: syntax error : missing ';' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2059: syntax error : ')'
g:/vc_projects/peronamalik/src/imageformat.h(58) : error C2143: syntax error : missing ')' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(58) : error C2143: syntax error : missing '{' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(58) : error C2371: 'BMP256' : redefinition; different basic types
g:/vc_projects/peronamalik/src/imageformat.h(55) : see declaration of 'BMP256'
g:/vc_projects/peronamalik/src/imageformat.h(58) : error C2143: syntax error : missing ';' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(58) : error C2059: syntax error : ')'
一开始以为自己对typedef struct用得不对,后来发现不是。这里
typedef struct tagBMP256{
BYTE *pData;
LONG pWidth;
LONG pHeight;
}BMP256;
作用等价于
struct tagBMP256{
BYTE *pData;
LONG pWidth;
LONG pHeight;
};
#define BMP256 struct tagBMP256
之前写过类似的程序
struct CFFT32
{
。。。
};
void CFFT32_invert(struct CFFT32 *sd)
问题应该不是这里。
网上搜了很多,也没真正解决问题。
后来才发现问题出现在我引用了windef.h这个头文件
去掉引用,再把相关的定义复制了过来
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef long LONG;
再次编译还是有错误:
--------------------Configuration: PeronaMalik - Win32 Debug--------------------
Compiling...
anisodiff2D.c
g:/vc_projects/peronamalik/src/imageformat.h(56) : error C2143: syntax error : missing ')' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(56) : error C2143: syntax error : missing '{' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(56) : error C2371: 'BMP256' : redefinition; different basic types
g:/vc_projects/peronamalik/src/imageformat.h(54) : see declaration of 'BMP256'
g:/vc_projects/peronamalik/src/imageformat.h(56) : error C2143: syntax error : missing ';' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(56) : error C2059: syntax error : ')'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2143: syntax error : missing ')' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2143: syntax error : missing '{' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2371: 'BMP256' : redefinition; different basic types
g:/vc_projects/peronamalik/src/imageformat.h(54) : see declaration of 'BMP256'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2143: syntax error : missing ';' before '*'
g:/vc_projects/peronamalik/src/imageformat.h(57) : error C2059: syntax error : ')'
检查问题出现在
void read_bmp256(FILE *fp, BMP256 *img);
我用了FILE这个结构体,却没有引用stdio.h头文件,加上,编译通过。
--------------------Configuration: PeronaMalik - Win32 Debug--------------------
Compiling...
anisodiff2D.c
anisodiff2D.obj - 0 error(s), 0 warning(s)
虽然问题解决了,但问题的实质还没有真正弄明白,生活上老是会碰上这样的问题,应该跟vc6编译器工作机制有关,有空再研究研究吧。