This page contains step-by-step instructions for compiling FreeType as a DLL.
FreeType DLL with MS Visual C
First of all, FreeType 2.1.10 (latest version) does not compile with MSVC: Microsoft compiler does not know about macro argument prescan, which is used in FreeType's 'otvalid' module. You have two ways to fix this:
- Remove 'otvalid' from the list of included modules. You do this by deleting the line "FT_USE_MODULE(otv_module_class)" in "include\freetype\config\ftmodule.h". By this way 'otvalid' module will be totally excluded from compilation and all code depending on it will fail.
Fix the 'otvalid' module by converting it to use normal simple macro syntax. You can use Werner's script to do this automatically. (I did it manually in my MSVC build of FreeType, although I am not totally sure its correct. Also, my modification breaks tracing in 'otvalid' module.)
Next you have to set a name export macro. Open file "include\freetype\config\ftoption.h", find this line:
/* #define FT_EXPORT(x) extern x */
and change it to:
#define FT_EXPORT(x) __declspec( dllexport ) x
Now, after you fixed 'otvalid' and export macro, you are ready to compile everything:
cl @_cl_options
Where file "_cl_options" contains:
/nologo /W3 /O2 /Ob1 /Za /EHsc /MD /c /Fo"objs/" /D"NDEBUG" /D"WIN32" /D"_LIB" /I"include" src/autofit/autofit.c src/base/ftapi.c src/base/ftbase.c src/base/ftbbox.c src/base/ftbdf.c src/base/ftbitmap.c src/base/ftdebug.c src/base/ftglyph.c src/base/ftinit.c src/base/ftmm.c src/base/ftotval.c src/base/ftpfr.c src/base/ftstroke.c src/base/ftsynth.c src/base/ftsystem.c src/base/fttype1.c src/base/ftwinfnt.c src/base/ftxf86.c src/bdf/bdf.c src/cache/ftcache.c src/cff/cff.c src/cid/type1cid.c src/gzip/ftgzip.c src/lzw/ftlzw.c src/otvalid/otvalid.c src/pcf/pcf.c src/pfr/pfr.c src/psaux/psaux.c src/pshinter/pshinter.c src/psnames/psmodule.c src/raster/raster.c src/sfnt/sfnt.c src/smooth/smooth.c src/truetype/truetype.c src/type1/type1.c src/type42/type42.c src/winfonts/winfnt.c
At this step you have to have MSVC environment set up, if you don't you can make a batch file that will first call "vsvars32.bat" and then the compilation command above.
Call this command in the root of FreeType installation (directory that has "src", "include", "README" etc). You can of course experiment with compiler options.
Now, the linking step:
link @_link_options
Where file "_link_options" contains:
/nologo /incremental:no /subsystem:windows /release /opt:ref /opt:icf,3 /opt:nowin98 /dll /out:"FreeType-2.1.10-MSVC-8b2.dll" /implib:"ft-msvc-dll.lib" objs/autofit.obj objs/ftapi.obj objs/ftbase.obj objs/ftbbox.obj objs/ftbdf.obj objs/ftbitmap.obj objs/ftdebug.obj objs/ftglyph.obj objs/ftinit.obj objs/ftmm.obj objs/ftotval.obj objs/ftpfr.obj objs/ftstroke.obj objs/ftsynth.obj objs/ftsystem.obj objs/fttype1.obj objs/ftwinfnt.obj objs/ftxf86.obj objs/bdf.obj objs/ftcache.obj objs/cff.obj objs/type1cid.obj objs/ftgzip.obj objs/ftlzw.obj objs/otvalid.obj objs/pcf.obj objs/pfr.obj objs/psaux.obj objs/pshinter.obj objs/psmodule.obj objs/raster.obj objs/sfnt.obj objs/smooth.obj objs/truetype.obj objs/type1.obj objs/type42.obj objs/winfnt.obj
That's all. Now you have a .lib file that you can copy to your MSVC "lib" directory, and a DLL that you can put somewhere in PATH. If you want different DLL file name, don't just rename it, change it in the link command.
This procedure was tested with MSVC 8.0 beta 2, but should also work with other versions, may be with some minor changes in compiler options.
FreeType DLL with MinGW
First step is setting a name export macro. Open file "include/freetype/config/ftoption.h", find this line:
/* #define FT_EXPORT(x) extern x */
and change it to:
#define FT_EXPORT(x) __declspec( dllexport ) x
Next you compile FreeType as usual. You can write a makefile or use a simple batch-script:
@call set_mingw.bat for %%f in (autofit/autofit.c base/ftapi.c base/ftbase.c base/ftbbox.c base/ftbdf.c base/ftbitmap.c base/ftdebug.c base/ftglyph.c base/ftinit.c base/ftmm.c base/ftotval.c base/ftpfr.c base/ftstroke.c base/ftsynth.c base/ftsystem.c base/fttype1.c base/ftwinfnt.c base/ftxf86.c bdf/bdf.c cache/ftcache.c cff/cff.c cid/type1cid.c gzip/ftgzip.c lzw/ftlzw.c otvalid/otvalid.c pcf/pcf.c pfr/pfr.c psaux/psaux.c pshinter/pshinter.c psnames/psmodule.c raster/raster.c sfnt/sfnt.c smooth/smooth.c truetype/truetype.c type1/type1.c type42/type42.c winfonts/winfnt.c ) do gcc -O3 -I"include" -D"NDEBUG" -c src/%%f
Now the linking step:
gcc -shared -o FreeType-2.1.10-MinGW-GCC-3.4.4.dll *.o -Wl,--output-def,ft.def gcc -shared -o FreeType-2.1.10-MinGW-GCC-3.4.4.dll *.o -Wl,--kill-at dlltool -d ft.def --dllname FreeType-2.1.10-MinGW-GCC-3.4.4.dll --output-lib libft-dll.a --kill-at
Done! Put .a file in MinGW/lib/ and .dll in PATH.
FreeType DLL with MinGW, easier method
In MinGW/MSYS, just run ./configure with an extra option:
tar zxvf freetype-2.2.1.tar.gz cd freetype-2.2.1 ./configure LDFLAGS='-no-undefined' make make install
It will create the .dll file automatically.
Please comment or modify this page if you find a mistake, or if you know some better ways.

