wiki:DevWikiWinMingWSys_20

Compiling PostGIS 2.0 under Windows using MinGW/Msys

A step by step guide to compiling PostGIS in Windows. This guide assumes that you already have a PostgreSQL installation on your computer. You will have to compile PostgreSQL here too, but that is just for the process of compiling PostGIS.

These instructions are using MingW 3.4.5 Msys 1.0. This is not the newest mingw which comes packaged with gcc 4.6+. We have issues with compiling with the new Msys that cause the build to crash and not be usable. These we are working on resolving.

For PostgreSQL 9.2+ both 32-bit and 64-bit builds are built using the mingw64 chain.

Install Directories

We have used the following install directories:

MinGW: c:\MinGW
Msys: c:\msys
sources c:\projects
install directory for postgresql: c:\projects\pg\pg90 (where pg90 is whatever your respective version is of PostgresQL)

Useful Utilities

A good tool for extracting tar-files is 7-zip from http://www.7-zip.org

1. Installing Minimalist GNU for Windows (MinGW)

Download Automated MinGW Installer 5.1.4 version from here (Current version from http://sourceforge.net/project/showfiles.php?group_id=2435)

  1. Run the setup file and choose everything (except possibly Ada)
  2. Accept the default Install location of C:\MinGW

2. Installing Minimal System (MSYS)

MSYS provides you with a unix shell environment for compiling code under windows.

Download MSYS version 1.0.11. http://sourceforge.net/projects/mingw/files/MSYS%20Base%20System/msys-1.0.11/MSYS-1.0.11.exe/download (this is listed in http://sourceforge.net/project/showfiles.php?group_id=2435)

  1. Say y to post Installation
  2. When prompted do you have MinGW -> y
  3. When prompted for where it is type -> C:\MinGW

3. Installing Mys Development Toolkit

Download the MysDTK1.0.1 from http://downloads.sourceforge.net/mingw/msysDTK-1.0.1.exe

  1. Run the EXE install and accept all the defaults

3b Install Make 3.8 or above

In order to compile postgresql 9.1alpha5 or above, you need make 3.8 or above. To confirm which version you have run:

make --version

If it's lower than 3.8, then download newer Msys make (note: Msys make not MingW make. The later version is in the newest Msys.

4. Installing GNUWin Support Packages

Download and install the following from http://gnuwin32.sourceforge.net/packages.html. Run each file and be sure to change the install directory to c:\MinGW

5. Update m4

  1. download from: http://prdownloads.sourceforge.net/mingw/m4-1.4.7-MSYS.tar.bz2?download
  2. untar and copy the m4.exe-file in the end of the path to C:\msys\1.0\bin

6. Update environment variables

The below is copied direct from http://www.mingw.org/wiki/HOWTO_Install_the_MinGW_GCC_Compiler_Suite#toc33

Environment Settings

When you install command line tools, such as MinGW, or !GnuWin32 tools, you have to tell the command line interpreter where to find them; this is usually accomplished by adding the appropriate directory names to the PATH variable in your environment. Typically, it is your responsibility to do this; please do not expect the installer, (if you used one), to do it for you.

Warning: Adding entries to the PATH is normally benign. However, if you delete, you may mess up your PATH string, and you could seriously compromise the functioning of your computer. Please be careful.

  1. Right-click on "My Computer" and select "Properties".
  2. Click Advanced -> Environment Variables.
  3. In the box entitled "System Variables" scroll down to the line that says "PATH" and double-click the entry.
  4. You will be presented with a dialog box with two text boxes, the bottom text box allows you to edit the PATH variable. It is very important that you do not delete the existing values in the PATH string, this will cause all sorts of problems for you!
  5. Scroll to the end of the string and at the end add ";<installation-directory>\bin". Here <installation-directory> is the full absolute path name of the installation directory; if you accepted the defaults when installing tar and gzip, then for these, it will (probably) be C:\Program Files\!GnuWin32, while for MinGW it will be C:\MinGW\bin, (if you kept to the recommended default installation directory). Don't forget the semicolon; this separates the entries in the PATH.
  6. press OK -> OK -> OK and you are done.

Set the environment variable HOME to C:\msys\1.0\home. Create it if it doesn't already exist.

7. Installing LibIconv

For iconv we are going to compile our own copy instead of installing from GNUWin.

Save the source in your C:\projects directory. Open up the MSYS terminal. Ignore the errors in the configure process.

# cd /c/projects
# tar xvfz libiconv-1.13.1.tar.gz
# cd libiconv-1.13.1
# ./configure --prefix=/c/mingw
# make
# make install

Verify if the version of iconv you are running is the same as the one you just compiled (1.13):

# iconv --version

8. Installing Autoconf, Automake and Libtool

Verify which versions of these you have with

autoconf --version etc.

If they are not newer than below then download and install. Download the source code from the GNU site:

Save the packages in the c:\projects folder.

Launch the MSYS terminal from Start->Programs->MinGW->msys or from the desktop MSYS icon.

# cd /c/projects
# mkdir pg
# mkdir postgresql
# mkdir geos
# mkdir proj
# ls

Now you will get the folders you have copied to the directory listed. The names of the folders will be different than in this example if you have newer releases of the tools. Configure, compile and install autoconf.

 tar xvfz autoconf-2.65.tar.gz
 cd autoconf-2.65
 ./configure --prefix=/c/mingw 
 make 
 make install
 autoconf --version

If it says you are running lower than 2.63, most likely you have another one installed in addition. Running the below will tell you which one it is picking up.

which autoconf

Now configure, compile and install automake.

# tar xvfz automake-1.11.tar.gz
# cd automake-1.11
# ./configure --prefix=/c/mingw
# make
# make install

Now configure, compile and install libtool.

# tar xvfz libtool-2.2.10.tar.gz
# cd libtool-2.2.10
# ./configure --prefix=/c/mingw
# make
# make install

9. Compiling PostgreSQL

Download latest PostgreSQL source and untar in C:\projects\postgresql

# cd /c/projects/postgresql
# tar xvfz postgresql-9.0.0.tar.gz
# cd postgresql-9.0.0
# ./configure --prefix=/c/projects/pg/pg90
# make
# make install

If you want to be able to do a full make check on PostGIS, Initialize the postgresql database cluster first.

Make sure to copy to C:\projects\pg\pg90\bin\libpq.dll from C:\projects\pg\pg90\lib

10. Compiling GEOS

Download latest GEOS source from http://trac.osgeo.org/geos/ either the http://download.osgeo.org/geos/geos-3.3.5.tar.bz2 and copy to c:\projects\geos .

cd /c/projects/geos
bzip2 -d -c geos-3.3.5.tar.bz2 | tar xvf -
cd geos-3.3.5
./configure --prefix=/c/projects/geos/rel-3.3.5
make
make install

To strip all the debug info weight from the libgeos DLL files, run

strip /c/projects/geos/rel-3.3.5/bin/*.dll

As the result is not compiled to the postgresql installation tree you have to copy everything from /c/projects/geos/rel-3.3.2/ to /c/projects/pg/pg90/

GEOS 3.3

If you are compiling the latest GEOS 3.3 (the SVN version), you may get an error because of a bug in MinGW. GEOS 3.3 is needed to take advantage of the new ST_MakeValid and several other new functions in PostGIS 2.0+ and also some fixes for topology exceptions in intersects and intersection.

I had to comment out the lines as seen below, lines 159 and 166 in C:\MinGW\include\c++\3.4.5\cwchar (the same issue still exists under gcc 4.4.0)

  //using ::swprintf;  
  //using ::vswprintf;

Refer to this MinGW bug ticket and mailing list entry for more details.

If you are runnning gcc 4.4.0 and you get complaints about link g++ you probably need to do this too. Change

library_names='libstdc++.dll.a'

to

#library_names='libstdc++.dll.a' 
library_names='libstdc++.a'

in file /c/mingw/lib/libstdc++.la, as alluded to here.

11. Compiling Proj4

Download Proj4 from the web site, and the datum shifts grid file.

and copy to c:\projects\proj

The datum grid file must be unzipped into the "nad" subdirectory of the Proj4 source tree.

 cd /c/projects/proj
 tar xvfz proj-4.6.1.tar.gz
 cd proj-4.6.1
 cd nad
 unzip ../../proj-datumgrid-1.5.zip
 cd ..
 ./configure --prefix=/c/projects/pg/pg90 --enable-shared --disable-static
 make
 make install

Make the libproj.dll and make it dynamically linked

 cd /c/projects/pg/pg90/lib
 gcc -shared -o libproj.dll -Wl,--out-implib=libproj.dll.a -Wl,--export-all-symbols -Wl,--enable-auto-import -Wl,--whole-archive libproj.a -Wl,--no-whole-archive /c/mingw/lib/libmingw32.a 

Note: The 4.7.0 version can be used, but causes problems. More information here and here.

You might have to set the PROJSO environment variable for the processes to find libproj.dll:

export PROJSO=/c/projects/pg/pg90/lib/libproj.dll

12. Installing C-Unit

C-Unit is a testing framework, and is needed by PostGIS 1.4 and above to run the "make check" testing step.

  1. Download http://sourceforge.net/projects/cunit/files/CUnit/CUnit-2.1-0-winlib.zip
  2. Extract and copy contents to C:\MinGW

13. Compiling LibXML2

  1. mkdir /c/projects/libxml
  2. cd /c/projects/libxml
  3. ftp://xmlsoft.org/libxml2/libxml2-2.7.8.tar.gz

There seems to be some finickiness with configure that sometimes you have to disable static binding and sometimes you don't it's still a mystery to me:

tar xvfz libxml2-2.7.8.tar.gz 
cd libxml2-2.7.8
./configure --prefix=/c/projects/libxml/libxml2-2.7.8release --enable-shared --disable-static
make
make install

-- to force to compile static if your GeomFromGML/KML crashes you can try this.

./configure --prefix=/c/projects/libxml/libxml2-2.7.8release --disable-shared CCFLAGS=LDFLAGS="-Wl,-static" CFLAGS=-O2
make
make install

LibXML if the above fails in producing a release =

Sometimes you get a libtoolT error at the end of configure which seems harmless. Sometimes it compiles on some machines and not others. I have concluded it has to do with which zlib if any it picks up (on one machine that worked it was picking up my tortoise git zlib and had a dependency on msvcr9). So you can explicitly set your paths with:

export PATH="/usr/local/bin:/mingw/bin:/bin"

and /or :

./configure --prefix=/c/projects/libxml/libxml2-2.7.8release --enable-shared --disable-static --without-zlib

As the result is not compiled to the postgresql installation tree you have to copy everything from /c/projects/libxml/libxml2-2.7.8release/ to /c/projects/pg/pg90/

14. Install GTK+ Bundle

If you are compiling the shp2pgsql-gui program, you will need the GTK+ bundle.

Unzip the bundle in c:\gtkbundle and then alter the system PATH so that c:\gtkbundle\bin is in the PATH.

15. Compiling GDAL

Download latest trunk http://download.osgeo.org/gdal/?C=M;O=D - at this time gdal180.zip or

svn checkout from https://svn.osgeo.org/gdal/trunk to c:\projects\gdal\trunk

cd /c/projects/gdal/trunk/gdal (if you svn checkout need to do an sh autogen.sh before can configure)

  • Edit GNUMakefile top part to get rid of ${GDAL_ROOT}
GDAL_OBJ = ./frmts/o/*.o \
            ./gcore/*.o \
            ./port/*.o \
            ./alg/*.o

cd gdal
./configure --with-libtiff=internal --with-libz=/c/gtkbundle --prefix=/c/projects/gdal/rel-trunk
make
make install

As the result is not compiled to the postgresql installation tree you have to copy everything from /c/projects/gdal/rel-trunk/ to /c/projects/pg/pg90/

16. GDAL ldconfig

In order for PostGIS to correctly use with GDALPolygonize (Needed for floating point rasters) -- need something like below set in your configure/compile script

export LDFLAGS="-L/c/projects/GDAL/rel-trunk/lib"

17. Compiling JSON-C

This is needed if you want ST_GeomFromGeoJSON to be functional. Download http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz and save to c:\projects\json-c

cd /c/projects/json-c
tar xvfz json-c-0.9.tar.gz
cd json-c-0.9
## had to compile with -w (disable warnings otherwise it gave errors
./configure --prefix=/c/projects/json-c/rel-0.9 CFLAGS=-w
make clean
make
make install

18. Compiling PostGIS

PostGIS 2.0

 cd /c/project/postgis
 tar xvfz postgis-2.0.6.tar.gz
 cd postgis-2.0.6
#this is need for shp2pgsql-gui compile
export PATH="/c/gtkbundle/bin:$PATH"
#to set to right postgres
export PATH="/c/projects/pg/pg90/bin/:$PATH"
(you'lll need to download docbook xsl files first)
export PGPORT=5439
 ./configure \
     --with-xml2config=/c/projects/libxml/libxml2-2.7.8release/bin/xml2-config \
     --with-pgconfig=/c/pg/pg90/bin/pg_config \
     --with-geosconfig=/c/projects/geos/rel-3.3.2/bin/geos-config \
     --with-projdir=/c/projects/pg/pg90\
     --with-gdalconfig=/c/projects/gdal/rel-trunk/bin/gdal-config \
     --with-jsondir=/c/projects/json-c/rel-0.9 \
     --with-gui --with-gettext=no \
     --with-xsldir=/c/projects/docbook

 make clean
 make
 make install
# make check

#this part is a hack, but again didn't work without it #you can try without doing this, but if you get errors about # conflicting type def during PostGIS compile, you need this. #In my case I got this:

c:/projects/json-c/rel-0.9/include/json/json_object.h:32: error: conflicting types for 'boolean'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/rpcndr.h:52: error: previous declaration of 'boolean' was here
  • Open up /c/projects/json-c/rel-0.9/include/json/json_object.h
  • remark out line 32 that reads

typedef int boolean

  • so it should now read

/** typedef int boolean; **/

  • During the PostGIS configure process, you have to put this back the way it was otherwise configure complains json.h or something is not usable and refuses to configure with JSON-C support. Then you remark the line out again during compile. Crazy I know, but json-c won't compile without that line so can't take it out before json-c compile.

If you run into problems with your build, pipe the output into a file for further analysis. The GUI requires that pkg-config be on your PATH, check that it is there by running 'which pkg-config'.

# make 2>&1 | tee /c/build.log

Might just be me, but to get PostGIS 2.0. to work under PostgreSQL 9.0, had to do these steps right after the ./configure and then follow the rest of the steps above. The PostgreSQL 9.0 mangled the ${PERL} global having it point at a strange place. The below hack assumes you already have Perl in search path and just replaces the $(PERL) placeholder with perl executable.

sed 's,$(PERL),perl,g' <postgis/Makefile >postgis/Makefile2
mv postgis/Makefile2 postgis/Makefile

Installing into PostgreSQL

  1. Copy the following files into your Windows PostgreSQL lib folder
    • C:\projects\pg90\lib\postgis-2.0.dll
  2. Copy the following files into your Windows PostgreSQL bin folder.
    • C:\projects\geos\rel-3.3.0\bin\libgeos-3-3-0.dll
    • C:\projects\geos\rel-3.3.0\bin\libgeos_c-1.dll
    • C:\projects\proj\rel-4.4.6\bin\libproj.dll,
    • libiconv-2.dll (from C:\MinGW\bin)
    • C:\projects\pg90\bin\shp2pgsql.exe
    • C:\projects\pg90\bin\pgsql2shp.exe
    • C:\projects\gdal\rel-trunk\lib\libgdal.dll
  3. If you compiled with-gui - you need to also copy the following files to PostgreSQL bin folder
    • shp2pgsql-gui.exe
    • And put gtk_bundle/bin in your path (if you need to distribute -- copy along the

gtk_bundle/bin and gtk_bundle/etc - hopefully this big footprint need will change for future versions)

  1. Copy the proj datum shift files to the PostgreSQL share/contrib/postgis/proj
  2. Open PGAdmin and make a new database without any template
  3. Open a new query-window to your new database
  4. Add PL/PgSQL procedural language to the new database: createlang plpgsql
  5. Run postgis.sql and spatial_ref_sys.sql from C:\projects\pg\share\contrib\

Hopefully you now have a database running your recently compiled postgis.

Last modified 11 years ago Last modified on 05/19/14 14:58:29
Note: See TracWiki for help on using the wiki.