linuxへの移植中に?な経験.
次の構造体なんですが,挙動がおかしい.
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
ファイルに保存してみると,bfTypeとbfSizeの間に2byteの空白ができてます.
色々と調べてみると,gccでは(?)x86では4byte毎に区切って構造体などを配置するようですね.
バイトアライメントというらしい.
VCと同じようにバイトアライメントを明示的に2byteにするには,
#pragma pack(push, 2)
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
#pragma pack(pop)
または,次のように間を詰めるように指定すればok.
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize __attribute__ ((packed));
WORD bfReserved1 __attribute__ ((packed));
WORD bfReserved2 __attribute__ ((packed));
DWORD bfOffBits __attribute__ ((packed));
} BITMAPFILEHEADER;
bfReserved1以降の __attribute__ ((packed))は今の場合は必要ないですが.
参考:Using and Porting the GNU Compiler Collection (GCC) – C 言語ファミリに対する拡張機能