参考链接:在Linux/Ubuntu下从源代码安装GDAL库
根据上面的参考链接进行安装gdal没有啥问题,编译后可以输出可执行程序,但是在运行的时候出现提示libgdal.so没有找到
编译出来运行异常解决方法
参考:
这里解决
demo源代码
测试Demo代码进行了简单修改,便于查看是否加载影像成功
#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!"); }
else {printf("Success open img!\n");}
GDALClose(poDataset);// 不关闭文件会得到段错误
return 0;
}
编译方法
1、单个cpp文件编译方法
g++ test.cpp -o test -lgdal
2、多个cpp文件编译方法
如果你该工程有多个cpp需编译,只需在test.cpp 后面空格追加多个cpp即可,例如
g++ main.cpp test1.cpp test2.cpp -o test -lgdal
3、很多很多个cpp文件编译方法
使用cmake进行编译
参考我以前写的博客:Linux cmake系统学习
异常的解决方法
还有Linux系统与windows系统略微存在一点差异,如果编译出错会有红色error提示,如:
解决方案:定位到源代码位置,将_isnan()这个函数用std::isnan()替换即可
又如:
提示说的很明显了,不能把字符串转换程std::ios_base::openmode
解决方案:把’w’字符串替换成std::ios_base::out 即可解决
以上,特此记录