みけCATのにっき(仮)
つれづれなるまゝに、日くらし、PCにむかひて、心に移りゆくよしなし事を、そこはかとなく書きつくれば、あやしうこそものぐるほしけれ。
(本当か!?)
出典

libjpegとwindows.hの併用

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

libjpegとwindows.hの併用

投稿記事 by みけCAT » 11年前

gcc4.7.0でlibjpegとwindows.hを併用すると、そのままではエラーが出たのでメモ。

最初、コードの上部はこのようになっていた。

CODE:

#include "bmpio.h"
#define XMD_H
#include 
#include 
#include 
#include "jpeglib.h"
bmpio.hは自作ライブラリのヘッダファイルで、中でwindows.hをインクルードしている。

この状態でコンパイルすると、

CODE:

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を追加。

CODE:

#include "bmpio.h"
#define XMD_H
#define HAVE_BOOLEAN
#include 
#include 
#include 
#include "jpeglib.h"
コンパイルが通って実行。あれ?jpegだけ出力されない。
調べてみると、コンソールにこのようなエラーが出力された。

CODE:

JPEG parameter struct mismatch: library thinks size is 376, caller expects 360
構造体のサイズが違うらしい。

ここで、前述のboolean型が被っているというエラーを思い出した。
gccの標準ではboolean型は定義されていない。
boolean型がどこで定義されているかを調べると、rpcndr.hというファイルの中に

CODE:

typedef unsigned char boolean;
というコードがあった。一方、libjpegのbooleanは

CODE:

#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だ。

CODE:

#include "bmpio.h"
#define XMD_H
#define HAVE_BOOLEAN
#include 
#include 
#include 
#define boolean int
#include "jpeglib.h"
こうすることによりコンパイルが通り、無事に正常なjpegファイルも出力された。

まとめ
windows.hとlibjpegを併用するときは、
windows.hをインクルードしたあと、jpeglib.hをインクルードする前に#define boolean intをするべし。

CODE:

#include 
#define boolean int
#include "jpeglib.h"

コメントはまだありません。