1.使用apt安装
sudo apt install g++
sudo apt install libgdal-dev
sudo apt install gdal-bin
2.验证
gdalinfo --version
# GDAL 3.0.4, released 2020/01/28
测试gdal_grid
、gdal_contour
命令等均可正常使用
3.CMake添加
# 查找 GDAL 库
find_package(GDAL REQUIRED)
# 打印找到的 GDAL 信息
message(STATUS "GDAL library found at: ${GDAL_LIBRARIES}")
message(STATUS "GDAL include directory: ${GDAL_INCLUDE_DIRS}")
# 链接 GDAL 库
target_link_libraries(ProjectName ${GDAL_LIBRARIES})
# 包含 GDAL 头文件
target_include_directories(ProjectName PRIVATE ${GDAL_INCLUDE_DIRS})
4.测试代码
#include <iostream>
#include <gdal.h>
int main()
{
GDALAllRegister();
const char* pszFilename = "dem.tif";
GDALDatasetH poDataset = (GDALDatasetH)GDALOpen(pszFilename, GA_ReadOnly);
if (poDataset == nullptr) {
std::cerr << "无法打开文件: " << pszFilename << std::endl;
return 1;
}
std::cout << "文件 " << pszFilename << " 已成功打开" << std::endl;
GDALClose(poDataset);
return 0;
}