User Tools

Site Tools


installation_guide:gdalonmsvc

Building GDAL under Windows and add it to a MSVC project

In here we will explain how to build GDAL under windows first and then how to include GDAL on a MSVC GDAL-test project.

There are several options for building GDAL under windows yourself:

  • Build all the dependencies yourself from source GDAL and link against the GDAL library
  • Download an SDK created by a very fine fellow by the name of Tamas Szekeres.

In here we go have chosen to build GDAL from source. To download and extract the latest GDAL source code, go to http://trac.osgeo.org/gdal/wiki/DownloadSource and copy the path to the version you would like to install. Here we show how to download and extract GDAL version 1.11.2 (Feb 2015). Using DOS command terminal just go to the folder where you want to download the tarball and run the commands shown below;

wget http://download.osgeo.org/gdal/1.11.2/gdal-1.11.2.tar.gz 
tar xzvf gdal-1.11.2.tar.gz

This will create a gdal-x.xx.x folder that contains, among others, the nmake.opt file that we need to edit with a text editor to tailor the installation to our compiler and set the path at which the build objects will be placed. For a MSVC version 2012 and WIN64 we make sure that the following is included in nmake.opt file:

!IFNDEF MSVC_VER
#assume msvc VS2012.
MSVC_VER=1700
!ENDIF

...

!IFNDEF GDAL_HOME
GDAL_HOME = "C:\gdal1112\1800-gdal-1.11.2"
!ENDIF

...

# Uncomment the following if you are building for 64-bit windows
# (x64). You'll need to have PATH, INCLUDE and LIB set up for 64-bit
# compiles.
WIN64=YES

Save the changes on nmake.opt and open a MSVC terminal prompt. If you have several command prompt to chose fromm, select the Native one that matches your OS (i.e. Native VS2012x64 on a x64bits OS). Go to the gdal-x.xx.x folder and un the following sequence of commands:

./configure
nmake makefile.vc
nmake makefile.vc install
nmake makefile.vc devinstall

If successful, you will have all the include, data, html, lib and bin folders in the directory you have specified as GDAL_HOME (i.e. C:\gdal1112\1700-gdal-1.11.2)

Run a GDAL test C++ script under MSVC

Copy the C++ script below and save it as a gdal_test.cpp file. This script just load the GDAL drivers and ask for a character to terminate the application. In the following we describe how this script is compile under MSVC2012.

#include <iostream>
using namespace std;
#include "gdal_priv.h"
int main ()
{
  std::cout << "Started...\n";
  // Register all available GDAL drivers
  GDALAllRegister();
  std::cout << "GDAL drivers loaded\n" << std::endl;
  std::cout << "Insert a character to close this window" << std::endl;
  char chardum;
  std::cin >> chardum;
  return 0;
}

Let´s create a new project from this existing file. Go to File–> New –> Project From Existing Code… We then select a visual C++ project type.

Specify the project location as the absolute path to the folder where the gdal_test.cpp is located. Automatically this path will be included into the folder list from which the project will be build. You need to provide a Project name (e.g. GDALTest) to go to Next.

Select Console Application Project when prompted how do you want to build the project.

Click Finish and the project solution explorer will be open.

Make sure that the platform matches the OS under which you have just have compiled GDAL (in the example below is WIN64 so we have chosen x64 as platform instead of Win32).

You now need to add additional Include directories, additional libraries and make sure that all required dlls are copied into the Debug folder where the gdal_test.exe will be build. All this can be done from the (project) Properties Pages.

  • To add additional Include directories go to Configuration Properties–> C/C++–> General –> Additional Include Directories and add the path to the gdal include folder (i.e. C:\gdal1112\1700-gdal-1.11.2\include).
  • To add additional libraries, go to Configuration Properties–> Linker–> General –> Additional Library Directories and add the path to the gdal lib. You will also need to add the additional dependencies Configuration Properties–> Linker–> Input–> Additional Dependencies and type gdal_i.lib
  • Copy the required DLLs into the Debug folder where the final executable will be build. This can be done Configuration Properties–> Build Events–> Pre-Link Event –> Command Line and type xcopy /y /d “C:\gdal1112\1700-gdal-1.11.2\bin\*.dll” “$(OutDir)”. Where C:\gdal1112\1700-gdal-1.11.2\bin\*.dll is just an example of the path to the gdal dlls.

Click F7 to build your executable GDALTest.exe into …/GDALTest/x64/Debug folder. To run the executable go to a DOS command prompt, navigate to the Debug folder location and just type the name of the executable file ''GDALTest“” and you should see:

installation_guide/gdalonmsvc.txt · Last modified: 2016/03/20 19:03 by coastalmeadmin