GDAL编译安装配置_超详细_SQLite_libtiff_CURL_PROJ_GEOS_HDF4_HDF5_HDF5_VS2022_C++

一、SQLite X64库安装与 测试(X86库同理)

下载网址: http://www.sqlite.org/download.html

下载项:sqlite-dll-win64-x64-xxxxx.zip、sqlite-amalgamation-xxxxx.zip、sqlite-tools-win32-x86-xxxxx.zip

1、新建SQLite库文件夹,并解压以上文件,解压方式为:amalgamation单独成一个文件夹(相当于include),另外两个解压至SQLite文件夹内即可;

 2、配置环境变量

将SQLite文件夹路径加入环境变量(如:D:/SQLite)

3、生成.lib文件

打开VS x64 本机工具命令提示符,进入SQLite文件夹、并编译;

1
2
3
4
D:
cd D:/SQLite
lib /def:sqlite3.def
( lib /def:sqlite3.def /machine:ix86 )

 完成后文件夹文件如下图:

 4、测试

VS新建测试项目,测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include "sqlite3.h"
 
int main(int argc, char* argv[])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
 
    rc = sqlite3_open("test.db", &db);
 
    if (rc) {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
 
    }
    else {
        fprintf(stderr, "Opened database successfully\n");
    }
    sqlite3_close(db);
 
    getchar();
    return 0;
}

 编译前需配置属性页;

VC++目录:包含目录为SQLite\amalgamation路径、库目录为SQLite路径

连接器->输入:附加依赖项加入sqlite3.lib

进行编译,编译成功界面:

参考 https://blog.csdn.net/weixin_51349575/article/details/119428093

二、安装libtiff库

1、下载地址:http://download.osgeo.org/libtiff/

2、用Cmake进行编译

3、打开tiff.sln工程文件

4、ALL_BUILD编译

5、进行INSTALL的生成

  如果INSTALL失败,需要对VS进行管理员启动(C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\VSLauncher.exe),可以右键属性,兼容性设置为管理员启动;

 6、配置环境变量(在C:\Program Files文件夹下生成tiff'文件夹,将bin文件夹添加到环境变量中)

三、安装CURL库

1、下载地址:https://github.com/curl/curl

2、同上Libtiff

四、编译PROJ

1、下载地址:https://github.com/OSGeo/PROJ

2、用Cmake进行编译

  如有错误,比如发现不到include文件等手动加入即可

  如果出现:

CMake Error at test/unit/CMakeLists.txt:32 (message):
  Build step for googletest failed: 1

    则,按提示网址下载复制到指定文件夹中即可

3、打开sln工程文件

4、ALL_BUILD编译

  如果找不到curl/curl.h文件,将CURL文件夹下的include路径加入到属性中(C/C++ -> 常规 -> 附加包含目录)(不要加上include的子文件夹curl,正确是:X:XX/CURL/include)

  如果出现curl解析错误,将libcurl-d_imp.lib文件路径加入到属性中 (链接器 -> 输入 -> 附加依赖项)

5、进行INSTALL的生成

6、添加环境变量

五、编译GEOS

1、下载:http://download.osgeo.org/geos/

2、同上

六、配置HDF4/HDF5

针对HDF5高版本不再包含ZLIB和SZIP库,对数据造成读取失败等问题,需要对HDF5进行源码编译!

0、编译并配置ZLIB、SZIP

下载:ZLIBSZIP

编译:同上

1、下载:

HDF5:https://support.hdfgroup.org/ftp/HDF5/releases/(下载源码SRC)

HDF:https://support.hdfgroup.org/ftp/HDF/releases/

2、编译安装

同上

七、编译GDAL(不出意外这一步骤非常快)

1、下载GDAL http://download.osgeo.org/gdal/

2、用Cmake进行编译

3、打开sln工程文件

4、ALL_BUILD编译

5、进行INSTALL的生成

6、添加环境变量

7、错误解决:

错误一:hdf5vfl.h(82,5): error C2440: “初始化”: 无法从“const char [5]”转换为“H5FD_class_value_t” .......

若hdf5vfl.h报错,则根据H5FD_class_t结构体的具体成员,在对应位置处添加数字nullptr

八、测试GDAL

1、新建项目

2、配置VS_GDAL环境

  • 加入属性

3、代码如下,D盘新增SHP文件即测试成功

复制代码
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include <ogrsf_frmts.h>
#include <gdal_priv.h>
#include <ogr_geometry.h>
#include <iostream> 
//生成shp文件
int main()
{
    const char* pszDriverName = "ESRI Shapefile";
    GDALDriver* poDriver;
    CPLSetConfigOption("SHAPE_ENCODING", "UTF-8"); //避免乱码
    GDALAllRegister();
    poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
    if (poDriver == NULL)
    {
        printf("%s driver not available.\n", pszDriverName);
        return 0;
    }
    GDALDataset* poDS;

    poDS = poDriver->Create("d:/newShp.shp", 0, 0, 0, GDT_Unknown, NULL); //创建shp文件
    if (poDS == NULL)
    {
        printf("Creation of output file failed.\n");
        return 0;
    }
    OGRLayer* poLayer;
    poLayer = poDS->CreateLayer("point_out", NULL, wkbPoint, NULL);
    if (poLayer == NULL)
    {
        printf("Layer creation failed.\n");
        return 0;
    }
    OGRFieldDefn idField("ID", OFTReal);
    OGRFieldDefn firstField("NAME", OFTInteger);
    OGRFieldDefn secondField("X", OFTReal);
    OGRFieldDefn thirdField("Y", OFTString);
    idField.SetWidth(32);
    firstField.SetWidth(32);
    secondField.SetWidth(32);

    poLayer->CreateField(&idField);
    poLayer->CreateField(&firstField);
    poLayer->CreateField(&secondField);
    poLayer->CreateField(&thirdField);


    int x, y;
    int a = 10, b = 100;
    for (int i = 1; i <= 10; i++)
    {
        OGRFeature* poFeature;
        poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());
        poFeature->SetField("ID", i);
        poFeature->SetField("NAME", i);
        x = (rand() % (b - a)) + a;
        y = (rand() % (b - a)) + a;
        poFeature->SetField("X", x);
        poFeature->SetField("Y", "你好,弟中弟");
        OGRPoint pt;
        pt.setX(x);
        pt.setY(y);
        poFeature->SetGeometry(&pt);
        if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)
        {
            printf("Failed to create feature in shapefile.\n");
            return 0;
        }
        OGRFeature::DestroyFeature(poFeature);
    }
    GDALClose(poDS);
    return 1;
}
复制代码

 

作者:HaijianYang
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

posted @   HaijianYang  阅读(1901)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示