1
FreeImage and MinGW / LCC WIN 32
2
================================
4
This file explains how to create a FreeImage.a file from the FreeImage DLL, so that it can be linked with the MinGW / LCC WIN 32 / GNU gcc C compiler.
6
1) Open a DOS shell (run the program cmd.exe)
8
2) Add the MinGW path to your environment variable :
10
C:\>set PATH=%PATH%;C:\mingw32\bin;
12
3) Creates a .def file from the FreeImage DLL
14
C:\>pexports freeimage.dll | sed "s/^_//" > freeimage_gcc.def
16
4) Creates the FreeImage.a file
18
C:\>dlltool -U -d freeimage_gcc.def -l freeimage.a
20
Note : you may also use a batch file to automate this process :
23
set PATH=%PATH%;C:\mingw32\bin;
24
pexports freeimage.dll | sed "s/^_//" > freeimage_gcc.def
25
dlltool -U -d freeimage_gcc.def -l libfreeimage.a
29
Here are some links on the use of WIN32 DLL with the GNU C compiler :
30
=====================================================================
34
A Moron's Guide to Using Microsoft DLL's when Compiling Cygwin or Mingw Programs
35
http://www.emmestech.com/software/cygwin/pexports-0.43/moron1.html
37
==> About GNU tools and DLL
39
Introduction to (mostly) GNU Programming Tools
40
http://webclub.kcom.ne.jp/ma/colinp/win32/tools/index.html
42
These pages are dealing with the use of Win32 DLL :
44
The DLL Import Library Tool
45
http://webclub.kcom.ne.jp/ma/colinp/win32/tools/dlltool.html
47
Using Dynamic Link Libraries
48
http://webclub.kcom.ne.jp/ma/colinp/win32/dll/use.html
1
=====================================================================
2
Using the FreeImage library with the MinGW Compiler Suite
3
=====================================================================
5
This file describes how to use the precompiled FreeImage library
6
FreeImage.dll with the MinGW port of the GNU Compiler Collection
7
(GCC), how to build this library from source using MinGW and how
8
to use this MinGW-built library with Microsoft Visual Studio.
14
1. Using the precompiled FreeImage library with MinGW
16
2. Building the FreeImage library with MinGW
18
3. Using the MinGW FreeImage library with Microsoft Visual Studio
23
---------------------------------------------------------------------
25
=====================================================================
27
The procedures described in this document have been developed and
28
tested using the following free tools:
30
1. MinGW GCC Version 4.4.0 (Core and C++ including required libs)
31
2. MinGW GNU Binutils Version 2.19.1
32
3. MinGW GNU Make Version 3.81-20080326-3
33
4. MinGW Runtime Version 3.15.2
34
5. MinGW API for MS-Windows Version 3.13
35
6. GnuWin32 Package CoreUtils Version 5.3.0 (only for building)
36
7. GnuWin32 Package Sed Version 4.2 (only for creating the GCC
39
* Sed is only needed to create a GCC-native import library from
40
the MSVC import library FreeImage.lib. However, since MinGW now
41
supports linking against MSVC lib files, this process seems to
42
be obsolete. See section 1.
44
Basically, no version dependent capabilities are used so, this
45
should also work with older versions of the tools mentioned above.
46
Similarly, the GnuWin32 packages (which I just prefer over MSYS)
47
could likely be replaced by a properly installed MSYS environment.
49
Furthermore, the following preconditions should be met:
51
1. The folders 'bin' under both the MinGW and the GnuWin32
52
installation directory should have been added to the PATH
53
environment variable. Likely it is best adding these
54
directories permanently to PATH through the System
55
Properties dialog on the Control Panel.
57
2. The MinGW Make package only provides a 'mingw32-make.exe'
58
executable. There is no alias 'make.exe'. However, make is
59
preconfigured to use 'make' as the default $(MAKE) command.
60
This seems to be a bug in the MinGW GNU Make distribution.
61
Thus, a copy of 'mingw32-make.exe' named 'make.exe' should
62
be placed into MinGW's 'bin' directory.
66
---------------------------------------------------------------------
67
1. Using the precompiled FreeImage library with MinGW
68
=====================================================================
70
When using functions from C/C++, that reside in a DLL, the linker
71
needs a so called import library, which specifies, how to
72
dynamically link these external functions during runtime. However,
73
different linkers use different types or formats of these import
76
Since the precompiled FreeImage library was build with Microsoft
77
Visual Studio, in the past, some extra work was required to use it
78
from MinGW. An import library, that was compatible with GNU ld,
79
must have been created first.
81
However, for several MinGW versions, the GNU linker ld also
82
supports linking against Microsoft Visual C++ import libraries
83
directly. So, this effectively makes any circulating HOWTO's on
84
how to create a GCC-compatible import library from a MSVC lib file
85
more or less obsolete. Additionally, MinGW does not require the
86
GCC/Linux usual lib prefix for libraries, so linking with MinGW
87
against the precompiled FreeImage DLL is as easy as with MSVC:
89
1.) Open a DOS shell (run application cmd.exe)
91
2.) Ensure, that the 'bin' folder of MinGW is added to the PATH
92
environment variable (see Prerequisites).
94
3.) Link directly against the supplied lib file:
96
C:\>gcc -oFreeImageTest.exe FreeImageTest.c -lFreeImage
98
Nonetheless, for the sake of completeness, the following steps
99
describe how to create a native GCC import library:
101
1.) Open a DOS shell (run application cmd.exe)
103
2.) Ensure, that the 'bin' folders of both MinGW and GnuWin32 are
104
added to the PATH environment variable (see Prerequisites).
106
3.) Create a .def file 'libfreeimage.def', that contains all symbols
107
exported by the FreeImage library:
109
C:\>pexports FreeImage.dll | sed "s/^_//" > libfreeimage.def
111
4.) Create the GCC compatible import library 'libfreeimage.a':
113
C:\>dlltool --add-underscore -d libfreeimage.def -l libfreeimage.a
115
5.) Use this library to link against with GCC:
117
C:\>gcc -oFreeImageTest.exe FreeImageTest.c -lfreeimage
121
---------------------------------------------------------------------
122
2. Building the FreeImage library with MinGW
123
=====================================================================
125
You *do not* need to have any other third party library (like
126
libjpeg, libpng, libtiff, libmng and zlib and others) installed on
127
your system in order to compile and use the library. FreeImage uses
128
its own versions of these libraries. This way, you can be sure that
129
FreeImage will always use the latest and properly tested versions
130
of of these third party libraries.
132
In order to build the FreeImage library under Windows with MinGW
133
(GCC), ensure that all the prerequisites mentioned above are met.
134
The MinGW makefile aims to build a Windows DLL, that differs as
135
least as possible from the precompiled library that comes with the
136
FreeImage distribution. Thus, the build process also includes the
137
DLL version resource as well as the __stdcall attribute for all the
138
exported functions, including the MSVC-like function decorations
141
When building the FreeImage DLL, of course, an import library is
142
generated, too. However, this input library is not in GCC's native
143
format, but in MSVC lib format, which makes it usable from both
144
MinGW and Microsoft Visual Studio with no further processing.
146
The MinGW makefile can also be used to build a static library.
147
However, due to the different function export attributes needed
148
for both the dynamic and the shared library (DLL), this requires
149
a separate invocation of make, which in turn needs to rebuild every
150
source file after switching from dynamic to static and vice versa.
151
So, a 'make clean' is required each time, the library type is
154
The type of library to build is specified by a variable named
155
FREEIMAGE_LIBRARY_TYPE, which may either be set directly in the
156
Makefile.mingw near line 18 or may be specified as an environment
157
variable. This variable may either take SHARED or STATIC to build
158
a dynamic link library (DLL) or a static library respectively.
159
Since this value is used to dynamically form the actual make target
160
internally, only uppercase values are valid. Defaults to SHARED.
162
The MinGW makefile also supports the 'install' target. However,
163
this only copies the FreeImage dynamic link library (DLL) from the
164
Dist folder into the %SystemRoot%\system32 folder. So, invoking this
165
target only makes sense, if the DLL has been built before.
167
Since there is neither a common system wide 'include' nor a 'lib'
168
directory available under Windows, the FreeImage header file
169
FreeImage.h as well as both the static library and the DLL import
170
library FreeImage.lib just remain in the 'Dist' folder.
172
The following procedure creates the FreeImage dynamic link library
173
(DLL) from the sources, installs it and also creates a static
176
1.) Open a DOS shell (run application cmd.exe)
178
2.) Ensure, that the 'bin' folders of both MinGW and GnuWin32 are
179
added to the PATH environment variable (see Prerequisites).
181
3.) Create the FreeImage dynamic link library (DLL):
185
4.) Install the FreeImage dynamic link library (DLL):
189
5.) Clean all files produced by the recent build process:
193
6.) Create a static FreeImage library:
195
C:\>set FREEIMAGE_LIBRARY_TYPE=STATIC
198
You should be able to link progams with the -lFreeImage option
199
after the shared library is compiled and installed. You can also
200
link statically against FreeImage.a from MinGW.
204
---------------------------------------------------------------------
205
3. Using the MinGW FreeImage library with Microsoft Visual Studio
206
=====================================================================
208
Since the MinGW makefile creates an import library in MSVC's lib
209
format, the produced shared library (DLL) can be used from both
210
MinGW and Microsoft Visual Studio with no further adaption. Just
211
link to the import library FreeImage.lib from either MinGW or
212
Microsoft Visual Studio.
216
---------------------------------------------------------------------
218
=====================================================================
220
- The MinGW homepage:
221
http://www.mingw.org/
223
- The GnuWin32 homepage:
224
http://gnuwin32.sourceforge.net/
226
- The GCC homepage and online documentation:
228
http://gcc.gnu.org/onlinedocs/
230
- The GNU Binutils homepage and online documentation:
231
http://www.gnu.org/software/binutils/
232
http://sourceware.org/binutils/docs-2.19/
234
- The GNU Make homepage and online documentation:
235
http://www.gnu.org/software/make/
236
http://www.gnu.org/software/make/manual/make.html