最初、コードの上部はこのようになっていた。 bmpio.hは自作ライブラリのヘッダファイルで、中でwindows.hをインクルードしている。
この状態でコンパイルすると、
In file included from jpeglib.h:26:0,
from jpeg.c:8:
jmorecfg.h:227:13: error: conflicting types for 'boolean'
In file included from C:\MinGW\include/objbase.h:4:0,
from C:\MinGW\include/ole2.h:9,
from C:\MinGW\include/windows.h:114,
from bmpio.h:4,
from jpeg.c:1:
C:\MinGW\include/rpcndr.h:52:23: note: previous declaration of 'boolean' was here
この時は深く考えず、HAVE_BOOLEANを追加。
#include "bmpio.h"
#define XMD_H
#define HAVE_BOOLEAN
#include
#include
#include
#include "jpeglib.h"
調べてみると、コンソールにこのようなエラーが出力された。 構造体のサイズが違うらしい。
ここで、前述のboolean型が被っているというエラーを思い出した。
gccの標準ではboolean型は定義されていない。
boolean型がどこで定義されているかを調べると、rpcndr.hというファイルの中に というコードがあった。一方、libjpegのbooleanは
#ifndef HAVE_BOOLEAN
typedef int boolean;
#endif
#ifndef FALSE /* in case these macros already exist */
#define FALSE 0 /* values of boolean */
#endif
#ifndef TRUE
#define TRUE 1
#endif
ではどうするか。typedefでは定義を上書きできない。
ならば、#defineだ。
#include "bmpio.h"
#define XMD_H
#define HAVE_BOOLEAN
#include
#include
#include
#define boolean int
#include "jpeglib.h"
まとめ
windows.hとlibjpegを併用するときは、
windows.hをインクルードしたあと、jpeglib.hをインクルードする前に#define boolean intをするべし。