Makefile.gnu for building gd-1.8.4 or gd-2.0.1 by Thomas Boutell,
on Cygwin, Gnu/Linux or MinGW/msys (untested).
Download the UNIX-line-endings source makefile.
Note: gd-1.8.4 is the latest stable release. Perl module "GD.pm" requires gd-1.8.4
according to its author, Lincoln Stein.
time of last modification:   
Fri 31 May at 04:15:54 PM UTC
NOTE:   gd-2.0.1 needs to be patched to build on Gnu-ish Win32 platforms, at least. Here's [plain ascii ] the patch [gz file ].
#   ***THIS MAKEFILE REQUIRES GNU MAKE***

#  "Makefile.gnu" (C) 2002 Soren Andersen
#   derived from "Makefile" in gd-2.0.1 package by Thomas Boutell
#  This Makefile has been adapted to build gd-1.8.x as well.

# Depending on your system and the libraries and features you have
# and want, you WILL need to modify the ARGUMENTS to this Makefile!
#      Some possible example arguments to `make':
#  SRCDIR=[where] PREFIX=[some_path] LIBPNG_OK=1 LIBFT2_OK=1
#  LIBJPEG_OK=1 XPM_OK=1 (and on Linux: USE_LDCONFIG=1)

# About "SRCDIR": this makefile supports building in a directory
# different from (outside, anywhere) the source directory. Default:
SRCDIR = .

# You must be using Gnu `make' to use this Makefile, so it is
# anticipated that all users will also be using gcc. This may
# change in the future.
COMPILER=gcc

# If the ar command fails on your system, consult the ar manpage
# for your system. 
AR=ar

# What installation utility do you prefer to use (gd comes with one)?
#   ** THIS might need editing **
#INSTALL_TOOL = $(SRCDIR)/install-item
INSTALL_TOOL = /usr/bin/install -m


# Where might you want to install gd when built?
PREFIX = /usr/local

# *******************************************************************************
#
# It is QUITE LIKELY that you will NOT NEED to make any CHANGES below this point,
# if you have followed the suggestions above about using the cmdline args.
#
# *******************************************************************************

# Old instructions from Thomas Boutell:
#Typical configuration: support for PNG images, JPEG images, and FreeType text.
#Remove -DHAVE_LIBFREETYPE if you can live without FreeType text.
#Add -DHAVE_XPM if you have X and xpm installed and you want that feature.
# -DHAVE_LIBPNG -DHAVE_LIBJPEG -DHAVE_LIBFREETYPE -DHAVE_XPM
# ---------------------------------------------------------------------------
# New instructions from Soren Andersen:
# Use arguments to `make' (see above) on the *cmdline* to alter all these
# settings in a coordinated manner. No makefile editing needed.
override CFLAGS += -O3 -Wall \
$(strip \
$(if $(LIBPNG_OK),-DHAVE_LIBPNG) \
$(if $(LIBFT2_OK),-DHAVE_LIBFREETYPE) \
$(if $(LIBJPEG_OK),-DHAVE_LIBJPEG) \
$(if $(XPM_OK),-DHAVE_XPM))

# Some automation set-up for the complex lib dependencies:
_auto_png := $(if $(LIBPNG_OK),$(shell libpng-config --version))
_auto_ft2 := $(if $(LIBFT2_OK),$(shell freetype-config --version))
# Appropriately-ordered flags for gcc `ld':
LIBS := $(strip -lgd \
$(if $(LIBPNG_OK),$(if $(_auto_png),$(shell libpng-config --ldflags --libs), \
 -lpng)) \
$(if $(LIBFT2_OK),$(if $(_auto_ft2),$(shell freetype-config --libs), \
 -lfreetype)) \
$(if $(LIBJPEG_OK),-ljpeg) \
$(if $(XPM_OK),-lm -lxpm,-lm))
#-lpng -lz -ljpeg -lfreetype -lm -lxpm

# Old:
#Typical install locations for freetype 2.0, zlib, xpm, libjpeg 
#and libpng header files. If yours are somewhere else, change this. 
#-I$(SRCDIR) is important to ensure that the version of gd you are installing 
#is used, and not an older release in your directory tree somewhere.
#-I/usr/include/freetype2 -I/usr/include/X11 -I/usr/X11R6/include/X11
#-I/usr/local/include
# ---------------------------------------------------------------------------
# New: For libpng and libfreetype2 on Cygwin & similar, there may be important
# build configuration assistance installed to the host system; USE IT.
INCLUDEDIRS := $(strip \
-I$(SRCDIR) -I- \
$(if $(LIBPNG_OK),$(if $(_auto_png),$(shell libpng-config --cppflags), \
 $(addprefix -I, \
 $(wildcard /usr/local/include/libpng /usr/include/libpng)))) \
$(if $(LIBFT2_OK),$(if $(_auto_ft2),$(shell freetype-config --cflags), \
 $(addprefix -I, \
 $(wildcard /usr/X11R6/include/freetype2 \
 /usr/local/include/freetype2 \
 /usr/include/freetype2)))) \
$(if $(XPM_OK),$(addprefix -I, \
 $(wildcard /usr/X11R6/include/X11 \
 /usr/include/X11))) \
)

# Old:
#Typical install locations for <STRIKEOUT>freetype</STRIKEOUT>, zlib, xpm
#and <STRIKEOUT>libpng</STRIKEOUT> libraries. If yours are somewhere else,
#other than a standard location such as /lib or /usr/lib, then change this.
#This line shouldn't hurt if you don't actually have some of the optional
#libraries and directories.
#-L/usr/local/lib -L/usr/lib/X11 -L/usr/X11R6/lib
# ---------------------------------------------------------------------------
# New: For most common win32 and gnu/linux installations, it is unlikely that
# any additions will be needed here. An exception might be to add an entry
#  '-I/usr/local/include' right after "strip" on the first line, esp. for some
# RedHat-derived linuxen? -- or p'raps you've got some headers from libraries
# you've installed yourself there, instead of in /usr/include. For MinGW, is
# anyone's guess where you've / they've put things... but under msys, s.w.oob.
# CYGWIN: *before enabling XPM* read the note at the end of this Makefile.
LIBDIRS := $(strip \
$(if $(XPM_OK), \
$(addprefix -I,$(wildcard /usr/include/X11 -I/usr/X11R6/include/X11))))

#Location where libgd.so|dll.a should be installed by "make install".
#THIS MUST BE ONE OF YOUR STANDARD SHARED LIBRARY LOCATIONS, unless
#you add a new directory to your LD_LIBRARY_PATH environment setting.
#Otherwise applications will NOT find libgd.so and will produce an 
#error.  
INSTALL_LIB      := $(addsuffix /lib,$(PREFIX))

#Location where .h files should be installed by "make install".
INSTALL_INCLUDE  := $(addsuffix /include,$(PREFIX))

#Location where useful non-test programs and the Win32 DLL executable
#should be installed by "make install".
INSTALL_BIN      := $(addsuffix /bin,$(PREFIX))

gd_version = grep -d skip -Ex -e '\ +gd [0-9]\.[0-9]\.[0-9].+' $(SRCDIR)/readme.txt  \
|sed 's,.*gd\ \([0-9]\.[0-9]\.[0-9]\).*,\1,'

VERSION       := $(strip $(shell $(gd_version)))
MAJOR_VERSION := $(shell echo $(VERSION) |sed -ne 's,\([0-9]\)\..*,\1,p')

# Obsoleted:
MAJOR_VERSION ?=2
VERSION       ?=2.0.1


#
#
# Changes should not be required below here.
#
#

VPATH = . $(SRCDIR)

EXE =
DLLPRE = lib
SOLIB  = .so
O := .o
A := .a
LDFLAGS  = -shared

define MinCyg_GCC
sed -n /'^gcc version '/s',.*\<\([A-Za-z]*\)\> \(special\|experimental\).*',\\1,p
endef
define Any_GCC
sed -n /'^gcc version '/s',.*\<\([A-Za-z]*\)\>.*',\\1,p
endef

ASK_SHELL_FIND_GCC := $(shell gcc -v 2>&1 | $(MinCyg_GCC))
 ifeq "$(ASK_SHELL_FIND_GCC)" ""
ASK_SHELL_FIND_GCC := $(shell gcc -v 2>&1 | $(Any_GCC))
 endif
 ifneq "$(ASK_SHELL_FIND_GCC)" ""
GCC_FLAVOR := $(strip $(ASK_SHELL_FIND_GCC))
 endif


#  now we use the info gleaned above to modify a few things.
 ifeq "$(GCC_FLAVOR)" "mingw"
WIN32FLAV := mingw
override CFLAGS  += -fnative-struct -funroll-loops
override LDFLAGS += -Bdynamic -Wl,-warn-once
EXE    = .exe
SOLIB  = .dll.a
STDBIN =# who knows? It's Windows -- Don't assume anything.
 endif
 ifeq "$(GCC_FLAVOR)" "cygwin"
WIN32FLAV := cyg
override CFLAGS  += -funroll-loops
override LDFLAGS += -Bdynamic -Wl,-warn-once
EXE    = .exe
DLLPRE = cyg
SOLIB  = .dll.a
STDBIN = /usr/bin/
   ifneq "$(filter XPM_OK%,$(MAKEOVERRIDES))" ""
SHELL = /usr/bin/bash
define UserReply
read -n 1 -p 'Choice: ' yes_xpm; echo $$yes_xpm;
endef
define Advise
You have supplied the arg XPM_OK to try to include XPM support in \
Cygwin.  Before proceeding make sure you actually understand all of \
the ramifications of this.  If you really want to proceed, type \
"y" now -- if not, type anything else.
endef
$(warning $(Advise))
ANSWER := $(strip $(shell $(UserReply) ))
     ifneq "$(ANSWER)" "y"
$(error Cancelled operation. Exiting now)
     else
$(warning Positive reply received. Going ahead with make.)
     endif
   endif
 endif

DLLVSUF = $(if $(WIN32FLAV),$(subst .,_,$(VERSION))$(SOLIB),$(SOLIB).$(VERSION))
CMMA = ,

CC= $(COMPILER) $(INCLUDEDIRS)
LD= $(COMPILER) -O3
DL_LDFLAGS= $(LIBDIRS) $(filter-out -lgd, $(LIBS))

#BIN_PROGRAMS= pngtogd pngtogd2 gdtopng gd2topng gd2copypal gdparttopng webpng
BIN_PROGRAMS = $(addsuffix $(EXE), \
pngtogd pngtogd2 gdtopng gd2topng gd2copypal gdparttopng webpng)
BIN_PROGRAMS += $(if $(XPM_OK),$(addsuffix $(EXE),bdftogd))

#TEST_PROGRAMS= gdtest gddemo gd2time gdtestft testac
TEST_PROGRAMS= $(addsuffix $(EXE),gdtest gddemo gd2time gdtestft testac)

  ifeq "$(MAJOR_VERSION)" "2"
LIBOBJS= $(addsuffix $(O),gd gd_gd gd_gd2 gd_io gd_io_dp \
  gd_io_file gd_ss gd_io_ss gd_png gd_jpeg gdxpm \
  gdfontt gdfonts gdfontmb gdfontl gdfontg gdtables gdft \
  gdcache gdkanji wbmp gd_wbmp gdhelpers gd_topal)
  else
LIBOBJS= $(addsuffix $(O),gd gd_gd gd_gd2 gd_io gd_io_dp \
  gd_io_file gd_ss gd_io_ss gd_png gd_jpeg gdxpm \
  gdfontt gdfonts gdfontmb gdfontl gdfontg gdtables gdft gdttf \
  gdcache gdkanji wbmp gd_wbmp gdhelpers)
TEST_PROGRAMS= $(addsuffix $(EXE),gdtest gddemo gd2time gdtestft gdtestttf)
  endif

PROGRAMS= $(BIN_PROGRAMS) $(TEST_PROGRAMS)

#  dependencies ---------------------------------------------------
.PHONY: default instructions all clean install clean-lib-objs
default: instructions

instructions:
     @echo -e '\n\n' Building gd version $(VERSION)'\n\n'; \
 echo First, maybe edit this Makefile. Read the comments at
     @echo the beginning and throughout, thoroughly.
     @echo
     @echo Second, type \'make all\' as root.
     @echo
     @echo This *installs* the GD $(VERSION) shared library,
     @echo which is required in order to use the included
     @echo utility programs, such as webpng, pngtogd, etc.
     @echo
     @echo OPTIONAL third step: type \'make test\' to build
     @echo the optional test programs.
     @echo Finally, to install the real tools, type \'make install\'.

PASSFLAG =
   ifeq "$(MAKELEVEL)" "0"
PASSFLAG = $(if $(wildcard *$(O)),,sbuild)
#O = $(if $(WIN32FLAV),.o,.pic.o)
   endif

test: $(TEST_PROGRAMS)

# A pattern-based rule for all the programs:
%$(EXE): %$(O)
     $(strip $(CC) $< -o $@ $(LIBDIRS) $(LIBS))

# -------------------------------------------------------------------------
# Major targets (ordinarily, only 'all' will be invoked).
static: libgd$(A)
win32_DLL = $(if $(WIN32FLAV),$(DLLPRE)gd.dll)
dynamic   = $(if $(WIN32FLAV),$(win32_DLL) libgd$(DLLVSUF),libgd$(DLLVSUF))
all: $(dynamic) $(BIN_PROGRAMS) static
# -------------------------------------------------------------------------

$(PROGRAMS): LIBS = -L$(INSTALL_LIB) -lgd$(subst .,_,$(VERSION))
   ifeq "$(MAKELEVEL)" "0"
$(LIBOBJS): override CFLAGS+= $(if $(WIN32FLAV),,-fPIC)
clean-lib-objs:
     -rm -f $(LIBOBJS)
   else
$(LIBOBJS): override CFLAGS = $(filter-out -fPIC,$(CFLAGS))
clean-lib-objs: ;
   endif

lib-objs: $(LIBOBJS)
     @echo -e '\n \
Accounted for: library object files for $(if $(PASSFLAG),dynamic,static) \
library.\n\n'
     @rm -f *_objs_done
     @touch $(if $(PASSFLAG),static,)_objs_done




# *****************************************************************
   ifneq "$(WIN32FLAV)" ""
# *****************************************************************
define LINK_STEP
     $(LD) $(LDFLAGS) \
 $(addprefix -Wl$(CMMA)-out-implib$(CMMA),$(word 2,$(dynamic))) \
 $(addprefix -Wl$(CMMA)-output-def$(CMMA), \
 $(subst $(SOLIB),.def,$(word 2,$(dynamic)))) \
 -Wl,--image-base=0x10000000 -o $(win32_DLL)
endef
define EXTRA_DLIB_ACTIONS
     -rm $(INSTALL_BIN)/$(win32_DLL)
     $(INSTALL_TOOL) 755 $(win32_DLL) \
  $(INSTALL_BIN)/$(win32_DLL)
     -rm $(INSTALL_LIB)/libgd$(SOLIB)
     ln -fs $(INSTALL_LIB)/libgd$(DLLVSUF) \
          $(INSTALL_LIB)/libgd$(SOLIB)
     -rm $(INSTALL_LIB)/libgd${MAJOR_VERSION}$(SOLIB)
     ln -fs $(INSTALL_LIB)/libgd$(DLLVSUF) \
  $(INSTALL_LIB)/libgd${MAJOR_VERSION}$(SOLIB)
endef
# *****************************************************************
   else
# *****************************************************************
define LINK_STEP
     $(LD) $(LDFLAGS) \
 $(addprefix -Wl$(CMMA)-soname$(CMMA),libgd.so.${MAJOR_VERSION}) \
 -o $(firstword $@)
endef
     ifndef USE_LDCONFIG
define EXTRA_DLIB_ACTIONS
     @echo -n Updating library linker name link \
      \"$(INSTALL_LIB)/libgd$(SOLIB)\" \
      to point to \"$(INSTALL_LIB)/libgd$(DLLVSUF)\"...
     -@rm $(INSTALL_LIB)/libgd$(SOLIB)
     @ln -fs $(INSTALL_LIB)/libgd$(DLLVSUF) \
          $(INSTALL_LIB)/libgd$(SOLIB) && echo done.
     @echo Updating library soname link \
      \"$(INSTALL_LIB)/libgd$(SOLIB).${MAJOR_VERSION}\" \
       to point to \"$(INSTALL_LIB)/libgd$(DLLVSUF)\"...
     -@rm $(INSTALL_LIB)/libgd$(SOLIB).${MAJOR_VERSION}
     @ln -fs $(INSTALL_LIB)/libgd$(DLLVSUF) \
          $(INSTALL_LIB)/libgd$(SOLIB).${MAJOR_VERSION} && echo done.
endef
     else
define EXTRA_DLIB_ACTIONS
     @pushd $(INSTALL_LIB) && /sbin/ldconfig -n . ; popd
endef
     endif
# *****************************************************************
   endif
# *****************************************************************


# Old:
#Shared library. This should work fine on any ELF platform (Linux, etc.) with
#GNU ld or something similarly intelligent. To avoid the chicken-and-egg
#problem, this target also installs the library so that applications can
#actually find it.
# ---------------------------------------------------------------------------
# New: Also works on Cygwin or MinGW.
# Note: at present, steps linking the shared lib and installing it are repeated twice.
# (only on Gnu-ish win32) I do not know why make is doing this but it doesn't matter
# much on my fast cpu.

$(dynamic): $(if $(wildcard sbuild_objs_done*),clean-lib-objs lib-objs,lib-objs)
     -rm -f $(dynamic)
     $(LINK_STEP) ${LIBOBJS} $(DL_LDFLAGS)
     $(INSTALL_TOOL) 644 libgd$(DLLVSUF) \
          $(INSTALL_LIB)/libgd$(DLLVSUF)
     @$(EXTRA_DLIB_ACTIONS)

# Static library, will be built by `make all' after the dynamic lib is compiled
# and installed, and the apps are built and linked to it. No static apps are built
# by this makefile. It would be easy enough to do so by simple invocations of
# the compiler piecemeal, for each app needed, after the `make' run has completed.
libgd$(A): staticlib-objects
     @rm -f libgd.a
     $(AR) rc libgd.a $(LIBOBJS)
     -ranlib libgd.a
     @echo $(clean_notice)
     @rm -f $(if $(WIN32FLAV),,)#$(LIBOBJS))


# Recursive invocation of `make' by itself, but only for a target (ELF) that
# distinguishes between relocatable obj code ("-fPIC") and not. All Win32 coff
# target object code is relocatable already (the "-fPIC" or "-fpic" flags will
# have no effect except to excite needless noises from the c pre-processor).

clean_notice := $(if $(WIN32FLAV),,Cleaned dir of static lib \
 object files$(CMMA) so that any future dynamic build run is not \
 contaminated by non-pic modules.)

  ifeq "$(WIN32FLAV)" ""
staticlib-objects: clean-lib-objs
     @echo -e '\n\n \
Rebuilding lib object files now for a *static* archive. \
 \n'
     $(MAKE) lib-objs -f Makefile.gnu $(MAKEFLAGS)
  else
staticlib-objects: lib-objs
     @rm -f _objs_done; touch sbuild_objs_done
  endif


# installation of binaries other than the win32 DLL executable (when applicable).
also_bdftogd = $(if $(XPM_OK), \
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),bdftogd) \
$(addsuffix $(EXE),$(INSTALL_BIN)/bdftogd), \
:)

install: install-headers install-binaries

install-binaries: $(Win32_DLL) $(BIN_PROGRAMS)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),pngtogd) \
$(addsuffix $(EXE),$(INSTALL_BIN)/pngtogd)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),pngtogd2) \
$(addsuffix $(EXE),$(INSTALL_BIN)/pngtogd2)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),gdtopng) \
$(addsuffix $(EXE),$(INSTALL_BIN)/gdtopng)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),gd2topng) \
$(addsuffix $(EXE),$(INSTALL_BIN)/gd2topng)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),gd2copypal) \
$(addsuffix $(EXE),$(INSTALL_BIN)/gd2copypal)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),gdparttopng) \
$(addsuffix $(EXE),$(INSTALL_BIN)/gdparttopng)
     $(INSTALL_TOOL) 755 $(addsuffix $(EXE),webpng) \
$(addsuffix $(EXE),$(INSTALL_BIN)/webpng)
     $(also_bdftogd)


install-headers:
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gd.h     )  $(INSTALL_INCLUDE)/gd.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gdcache.h)  $(INSTALL_INCLUDE)/gdcache.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gd_io.h  )  $(INSTALL_INCLUDE)/gd_io.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gdfontg.h)  $(INSTALL_INCLUDE)/gdfontg.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gdfontl.h)  $(INSTALL_INCLUDE)/gdfontl.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gdfontmb.h) $(INSTALL_INCLUDE)/gdfontmb.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gdfonts.h)  $(INSTALL_INCLUDE)/gdfonts.h
     $(INSTALL_TOOL) 644 $(addprefix $(SRCDIR)/,gdfontt.h)  $(INSTALL_INCLUDE)/gdfontt.h


clean:
     rm -f *$(O) *$(A) *$(SOLIB) $(win32_DLL) *.def \
         $(PROGRAMS) test/gdtest.jpg test/gdtest.wbmp *_objs_done


# *************************************************************************
# About enabling XPM ("XPM_OK") on Cygwin:
#
# You can either link to the xpm-nox library (in /usr/lib/nox/ or to the 
# "real" X-based xpm library (in /usr/X11R6/lib).  If you choose the 
# X-based one, then you'll have to have an Xserver running when you use 
# your application. 
# See  /usr/doc/Cygwin/xpm-nox-4.2.0.README
# 
# In short, it's quite messy and unless you *really* need it, you might
# as well save yourself the strain.
#
# *************************************************************************