在Linux/Ubuntu下从源代码安装GDAL库

  • by

GDAL库是一个开源的、方便读写处理栅格数据的C++库,
(当然也支持Python, Perl, Ruby, R, Java, C#等语言)
在Linux/Ubuntu下从源代码安装过程如下:

准备

  1. 确认电脑上安装好g++等开发环境,否则执行
    sudo apt-get install build-essential
    
  2. 确认电脑上已经安装好svn以便下载最新的源代码,否则执行
    sudo apt-get install subversion
    
  3. (可选)如果需要读取hdf4文件,需要安装libhdf4-dev库,执行
    sudo apt-get install libhdf4-dev
    
  4. (可选,推荐)如果需要安装基于python的预置处理工具(如gdal_mergy.py),需要安装python-dev库,执行
    sudo apt-get install python-dev
    

获取源代码

在保存代码的目录执行:

svn checkout https://svn.osgeo.org/gdal/trunk/gdal gdal_source

即可开始下载源代码,并保存到gdal_source目录中去

编译安装

进入源代码目录

cd gdal_source 

首先配置makefile,如果需要hdf4文件支持,输入

./configure --with-hdf4=yes

若不需要hdf4文件支持,或者没有安装libhdf4-dev库则只需要输入

./configure

即可

如果出现错误,就根据提示内容继续安装缺失的依赖包;
如果一切正常,则输入
make
开始编译gdal库,若编译正常,则输入
sudo make install
相应文件将会复制到系统目录中,安装完成

样例程序

样例程序_test.cpp_如下:

test.cpp ++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
#include <cstdio>
#include <cstdlib>
int main()
{
    GDALDataset  *poDataset;
    GDALAllRegister();
    poDataset = (GDALDataset *) GDALOpen("somefile.tiff" , GA_ReadOnly );
    if( poDataset == NULL )
    {
        printf("Open file failed!");
    }
    GDALClose(poDataset);// 不关闭文件会得到段错误
    return 0;
}

 

编译时添加-lgdal选项即可,如下:

g++ test.cpp -o test -lgdal

更多教程请阅读GDAL tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.