在Visual Studio 2010平台下配置GDAL库,GDAL库链接,示例的库版本为GDAL 1.7.3。
主要步骤
1. 使用Visual Studio 2010新建一个工程
新建完成后运行一次。
2. 准备GDAL库
一般来讲,GDAL库会放在一个固定的位置,但是为了程序的可移植性,可以尝试将GDAL库放置在新建的工程之中。找到gdaltest文件夹,然后找到gdaltest同名文件夹,将GDAL1.7.3库拷贝至其中。
3. 属性页配置
打开项目—>属性—>配置属性—>VC++目录,修改包含目录和库目录:
包含目录中,选址GDAL库中的include文件夹(绝对路径)。
但是为了方便程序的移植,这里可以改成相对路径(GDAL1.7.3\include)
库目录也设置成相对路径(GDAL1.7.3\lib)
再打开项目—>属性—>配置属性—>C/C++—>常规,在附加包含目录中配置包含文件(GDAL1.7.3\include)
4. 将GDAL\bin中的文件拷至Debug中
将GDAL\bin中gdal17.dll文件和geos_c.dll文件拷贝至Debug中。
注:Debug的目录为F:\visual studio 2010 Projects\gdaltest\Debug,而不是F:\visual studio 2010 Projects\gdaltest\gdaltest\Debug。
5. 运行测试
// gdaltest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "gdal_priv.h"
#pragma comment(lib,"gdal_i.lib")
#include "ogr_geometry.h"
int _tmain(int argc, _TCHAR* argv[])
{
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
CPLSetConfigOption("USE_RRD","YES");
const char* path = "data//test.tif";
GDALDataset * poDataset = (GDALDataset *) GDALOpen(path, GA_ReadOnly);
if(poDataset == NULL)
{
printf("open file fail.\n");
return false;
}
int nImgWidth = poDataset -> GetRasterXSize();
int nImgHeight = poDataset -> GetRasterYSize();
int nBandnum = poDataset -> GetRasterCount();
double *dGeoTrans = new double [6];
poDataset -> GetGeoTransform(dGeoTrans);
printf("Bandnum: %d \n", nBandnum);
printf("ImgWidth: %d \n", nImgWidth);
printf("ImgHeight: %d \n", nImgHeight);
for (int i = 0; i < 6; ++i)
{
printf("GeoTrans %d : %.2f \n", i + 1, dGeoTrans[i]);
}
GDALClose(poDataset);
delete [] dGeoTrans;
system("pause");
return 0;
}
输出了影像的基本信息,意味着GDAL库配置成功。
欢迎大家批评指正。