Code::Blocks Forums

User forums => Help => Topic started by: Dbug on August 13, 2008, 10:03:40 pm

Title: Problems using the FreeImage library
Post by: Dbug on August 13, 2008, 10:03:40 pm
Hi.

I'm still in my quest toward switching my pet projects from Visual Studio to Code::Blocks, and apparently the last hurdle I have (well, until another one happens :D) is to manage rebuilding my picture converter using Code::Blocks.

This converter (PictConv: http://www.defence-force.org/computing/oric/coding/annexe_3/pictconv (http://www.defence-force.org/computing/oric/coding/annexe_3/pictconv)) is using the FreeImage library (http://freeimage.sourceforge.net (http://freeimage.sourceforge.net)) to load and save the pictures.

The oldest versions of FreeImage were coded in C, so it was not a problem to link the .lib file in any particular compiler. Unfortunately (or not, it's a good thing that the library evolved) the library is now using C++ internally, with exceptions and stuff, and as a result the library file is not accepted by mingw, got a lot of undefined references:

-------------
libs\FreeImage.lib(./release/FIRational.obj):(.text$x+0x4): undefined reference to `??1exception@std@@UAE@XZ'
libs\FreeImage.lib(./release/FIRational.obj):(.text$x+0xe): undefined reference to `___CxxFrameHandler3'
libs\FreeImage.lib(./release/FIRational.obj):(.text[?str@?$basic_stringbuf@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@XZ]+0x115): undefined reference to `??3@YAXPAX@Z'

-------------

So well, I downloaded the latest source code (http://downloads.sourceforge.net/freeimage/FreeImage3110.zip (http://downloads.sourceforge.net/freeimage/FreeImage3110.zip)) and try to rebuild the library myself.

Code::Blocks imported the SLN file without any problem, the whole project appears exactly like in visual studio, but unfortunately mingw can't compile it due to apparently a lot of "trickery" in the header files:

-------------
../FreeImage.h:156: error: conflicting declaration 'typedef uint32_t DWORD'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/windef.h:229: error: 'DWORD' has a previous declaration as `typedef long unsigned int DWORD'
../FreeImage.h:156: error: declaration of `typedef uint32_t DWORD'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/windef.h:229: error: conflicts with previous declaration `typedef long unsigned int DWORD'
../FreeImage.h:156: error: declaration of `typedef uint32_t DWORD'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/windef.h:229: error: conflicts with previous declaration `typedef long unsigned int DWORD'
../FreeImage.h:157: error: conflicting declaration 'typedef int32_t LONG'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/winnt.h:79: error: 'LONG' has a previous declaration as `typedef long int LONG'
../FreeImage.h:157: error: declaration of `typedef int32_t LONG'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/winnt.h:79: error: conflicts with previous declaration `typedef long int LONG'
../FreeImage.h:157: error: declaration of `typedef int32_t LONG'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/winnt.h:79: error: conflicts with previous declaration `typedef long int LONG'
../FreeImage.h:173: error: redefinition of `struct tagRGBQUAD'
e:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/wingdi.h:1403: error: previous definition of `struct tagRGBQUAD'
../FreeImage.h:184: error: ISO C++ forbids declaration of `RGBQUAD' with no type
../FreeImage.h:184: error: conflicting declaration 'typedef int RGBQUAD'

-------------

The reason is that FreeImage is using some Windows types in the public API, and since they do not exist on Mac and Linux, they had to redefine the types in "freeimage.h":

-------------
typedef struct tagRGBQUAD {
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
  BYTE rgbBlue;
  BYTE rgbGreen;
  BYTE rgbRed;
#else
  BYTE rgbRed;
  BYTE rgbGreen;
  BYTE rgbBlue;
#endif // FREEIMAGE_COLORORDER
  BYTE rgbReserved;
} RGBQUAD;
-------------

The problem is that mingw apparently did something similar, with the same types defined in 'wingdi.h', but not exactly as the original MS header files did it.

So as a result, due to the order of inclusion of the various header files, the whole system is not working under mingw.

I tried to "fix" the ones in wingdi.h to solve the problem (by doing some ugly magic), but then it failed on some other structure in some other files.

I'm posting that here instead of on the mingw list because people here seem more reactive :)

So, if anyone has an idea on how to solve this issue, this would be great !

Thanks a lot :)

Title: Re: Problems using the FreeImage library
Post by: MortenMacFly on August 14, 2008, 08:48:34 am
Why don't you post in the FreeImage forums?
This is neither MinGW nor C::B related.
Title: Re: Problems using the FreeImage library
Post by: ironhead on August 15, 2008, 01:51:11 pm
Adding a guard around the conflicting definitions such as:

Code
#ifndef __MINGW32__
typedef uint32_t DWORD;
....
#endif

in FreeImage.h should fix the problem.
Title: Re: Problems using the FreeImage library
Post by: Dbug on August 18, 2008, 08:48:52 pm
Why don't you post in the FreeImage forums?
This is neither MinGW nor C::B related.

Because some people last year posted there already, and had almost no interesting answers, but I tried again:
http://sourceforge.net/forum/forum.php?thread_id=2170332&forum_id=36111 (http://sourceforge.net/forum/forum.php?thread_id=2170332&forum_id=36111)

No answer so far.

The Code::Blocks forum is much more reactive :)

Title: Re: Problems using the FreeImage library
Post by: Walking_Target on January 03, 2009, 04:38:54 am
The problem here is a caused by a discrepancy between Visual Studio and MinGW in their versions of windows.h.

In Visual Studio 2005, windows.h contains the following preprocessor directives:
Code
#ifndef _WINDOWS_
#define _WINDOWS_

In the header files distributed with the MinGW installation of Code::Blocks, the beginning of windows.h looks like this:
Code
#ifndef _WINDOWS_H
#define _WINDOWS_H

FreeImage DOES check for a previous inclusion of windows.h to prevent redefinition, but does it using the _WINDOWS_ constant (ala Visual Studio).