zoukankan      html  css  js  c++  java
  • mingw 构建 gdal 2.1.2

    前两日有人在oschian上问我mingw编译gdal的一些问题,然后我简单的编译了一次,记录下编译过程。
    此文是2017年5月写的,当时存在草稿箱,今天才看到。也许已经不适用于当前最新的版本了。

    1、准备

    1、安装mingw,我安装的是mingw-w64的x86_64-7.1.0-win32-seh-rt_v5-rev0版本。
    2、准备一个类UNIX模拟运行环境。我这里直接使用了git自带的msys,你也可以下载cygwin。
    3、下载并解压GDAL源码

    注意:mingw的安装路径必须没有空格,如果有,后面编译的时候会找不到编译器(必须强制指定绝对路径,路径需要用引号括起来)。GDAL的源码解压路径也不要有空格。

    2、生成Makefile

    在msys环境下,进入gdal-2.1.2源码目录,运行一下命令生成GNUMakefile文件

    # 将mingw的路径导入环境变量PATH
    export PATH=$PATH:/c/mingw-w64/x86_64-7.1.0-win32-seh-rt_v5-rev0/mingw64/bin
    # 生成Makefile文件
    ./configure --prefix=/z/gdal_2.1.2_mingw64 --without-libtool
    ## --prefix 指定安装路径
    ## --without-libtool 不使用libtool(因为我当前环境没有)
    ## 默认是静态库和动态库都生成,使用libtool可能只能生成静态库,因为它不知道在当前平台如何生成动态库。
    

    注意
    上述过程没有错误的话,会正常生成GNUMakefileGDALmake.opt文件。
    因为我们安装的mingw64并不能识别/c/xxx这样的路径(这是msys下的路径),所以需要修改GDALmake.opt文件,将其中的GDAL_ROOT的值修改为C:xxx这种形式。

    3、编译

    运行下面命令编译

    mingw32-make
    

    4、编译遇到错误及解决办法

    编译过程中出现了下面错误

    1、生成静态库时候ar提示参数列表太长

    ar r Z:/gdal-2.1.2/libgdal.a Z:/gdal-2.1.2/frmts/o/*.o Z:/gdal-2.1.2/gcore/*.o Z:/gdal-2.1.2/port/*.o Z:/gdal-2.1.2/alg/*.o Z:/gdal-2.1.2/apps/commonutils.o Z:/gdal-2.1.2/apps/gdalinfo_lib.o Z:/gdal-2.1.2/apps/gdal_translate_lib.o Z:/gdal-2.1.2/apps/gdalwarp_lib.o Z:/gdal-2.1.2/apps/ogr2ogr_lib.o Z:/gdal-2.1.2/apps/gdaldem_lib.o Z:/gdal-2.1.2/apps/nearblack_lib.o Z:/gdal-2.1.2/apps/gdal_grid_lib.o Z:/gdal-2.1.2/apps/gdal_rasterize_lib.o Z:/gdal-2.1.2/apps/gdalbuildvrt_lib.o Z:/gdal-2.1.2/ogr/ogrsf_frmts/o/*.o ./ogr/ogrgeometryfactory.o ./ogr/ogrpoint.o ./ogr/ogrcurve.o ./ogr/ogrlinestring.o ./ogr/ogrlinearring.o ./ogr/ogrpolygon.o ./ogr/ogrutils.o ./ogr/ogrgeometry.o ./ogr/ogrgeometrycollection.o ./ogr/ogrmultipolygon.o ./ogr/ogrsurface.o ./ogr/ogrmultipoint.o ./ogr/ogrmultilinestring.o ./ogr/ogrcircularstring.o ./ogr/ogrcompoundcurve.o ./ogr/ogrcurvepolygon.o ./ogr/ogrcurvecollection.o ./ogr/ogrmulticurve.o ./ogr/ogrmultisurface.o ./ogr/ogr_api.o ./ogr/ogrfeature.o ./ogr/ogrfeaturedefn.o ./ogr/ogrfeaturequery.o ./ogr/ogrfeaturestyle.o ./ogr/ogrfielddefn.o ./ogr/ogrspatialreference.o ./ogr/ogr_srsnode.o ./ogr/ogr_srs_proj4.o ./ogr/ogr_fromepsg.o ./ogr/ogrct.o ./ogr/ogr_opt.o ./ogr/ogr_srs_esri.o ./ogr/ogr_srs_pci.o ./ogr/ogr_srs_usgs.o ./ogr/ogr_srs_dict.o ./ogr/ogr_srs_panorama.o ./ogr/ogr_srs_ozi.o ./ogr/ogr_srs_erm.o ./ogr/swq.o ./ogr/swq_expr_node.o ./ogr/swq_parser.o ./ogr/swq_select.o ./ogr/swq_op_registrar.o ./ogr/swq_op_general.o ./ogr/ogr_srs_validate.o ./ogr/ogr_srs_xml.o ./ogr/ograssemblepolygon.o ./ogr/ogr2gmlgeometry.o ./ogr/gml2ogrgeometry.o ./ogr/ogr_expat.o ./ogr/ogrpgeogeometry.o ./ogr/ogrgeomediageometry.o ./ogr/ogr_geocoding.o ./ogr/osr_cs_wkt.o ./ogr/osr_cs_wkt_parser.o ./ogr/ogrgeomfielddefn.o ./ogr/ograpispy.o
    /usr/bin/sh: /c/mingw-w64/x86_64-7.1.0-win32-seh-rt_v5-rev0/mingw64/bin/ar: Argument list too long
    GNUmakefile:47: recipe for target 'Z:/gdal-2.1.2/libgdal.a' failed
    mingw32-make[1]: *** [Z:/gdal-2.1.2/libgdal.a] Error 126
    

    这个问题可以修改GNUMakefile文件中的GDAL_OBJ变量来进行,只需要将原本的一个GDAL_OBJ拆分为多个,然后对GDAL_SLIB目标的动作进行修改,改为生成多个静态库,然后合成一个即可。
    因为这个修改量比较大,我就不修改了,直接在外面使用命令生成libgdal.a。我这里源码目录是Z:/gdal-2.1.2

    # 先将其分作四部分,生成四个静态库
    # 注意 ar 生成这四个静态库的参数是 sr
    ar sr  basepart.a Z:/gdal-2.1.2/frmts/o/*.o Z:/gdal-2.1.2/gcore/*.o Z:/gdal-2.1.2/port/*.o Z:/gdal-2.1.2/alg/*.o 
    
    ar sr  appspart.a Z:/gdal-2.1.2/apps/commonutils.o Z:/gdal-2.1.2/apps/gdalinfo_lib.o Z:/gdal-2.1.2/apps/gdal_translate_lib.o Z:/gdal-2.1.2/apps/gdalwarp_lib.o Z:/gdal-2.1.2/apps/ogr2ogr_lib.o Z:/gdal-2.1.2/apps/gdaldem_lib.o Z:/gdal-2.1.2/apps/nearblack_lib.o Z:/gdal-2.1.2/apps/gdal_grid_lib.o Z:/gdal-2.1.2/apps/gdal_rasterize_lib.o Z:/gdal-2.1.2/apps/gdalbuildvrt_lib.o
    
    ar sr ogrpart1.a Z:/gdal-2.1.2/ogr/ogrsf_frmts/o/*.o ./ogr/ogrgeometryfactory.o ./ogr/ogrpoint.o ./ogr/ogrcurve.o ./ogr/ogrlinestring.o ./ogr/ogrlinearring.o ./ogr/ogrpolygon.o ./ogr/ogrutils.o ./ogr/ogrgeometry.o ./ogr/ogrgeometrycollection.o ./ogr/ogrmultipolygon.o ./ogr/ogrsurface.o ./ogr/ogrmultipoint.o ./ogr/ogrmultilinestring.o ./ogr/ogrcircularstring.o ./ogr/ogrcompoundcurve.o ./ogr/ogrcurvepolygon.o ./ogr/ogrcurvecollection.o ./ogr/ogrmulticurve.o ./ogr/ogrmultisurface.o ./ogr/ogr_api.o ./ogr/ogrfeature.o ./ogr/ogrfeaturedefn.o ./ogr/ogrfeaturequery.o ./ogr/ogrfeaturestyle.o ./ogr/ogrfielddefn.o ./ogr/ogrspatialreference.o ./ogr/ogr_srsnode.o ./ogr/ogr_srs_proj4.o ./ogr/ogr_fromepsg.o ./ogr/ogrct.o 
    
    ar sr ogrpart2.a ./ogr/ogr_opt.o ./ogr/ogr_srs_esri.o ./ogr/ogr_srs_pci.o ./ogr/ogr_srs_usgs.o ./ogr/ogr_srs_dict.o ./ogr/ogr_srs_panorama.o ./ogr/ogr_srs_ozi.o ./ogr/ogr_srs_erm.o ./ogr/swq.o ./ogr/swq_expr_node.o ./ogr/swq_parser.o ./ogr/swq_select.o ./ogr/swq_op_registrar.o ./ogr/swq_op_general.o ./ogr/ogr_srs_validate.o ./ogr/ogr_srs_xml.o ./ogr/ograssemblepolygon.o ./ogr/ogr2gmlgeometry.o ./ogr/gml2ogrgeometry.o ./ogr/ogr_expat.o ./ogr/ogrpgeogeometry.o ./ogr/ogrgeomediageometry.o ./ogr/ogr_geocoding.o ./ogr/osr_cs_wkt.o ./ogr/osr_cs_wkt_parser.o ./ogr/ogrgeomfielddefn.o ./ogr/ograpispy.o
    
    # 然后合成一个
    ar r Z:/gdal-2.1.2/libgdal.a basepart.a appspart.a ogrpart1.a ogrpart2.a
    

    注意:前面生成四个静态库的时候使用的参数是sr,这表示生成的是一个ranlib(仅打包.o文件,而不处理.o中的符号表)。后面生成libgdal.a的时候,使用的是r参数,表示打包后还将对静态库的成员__.SYMDEF进行更新。

    2、生成动态库时候g++提示参数列表太长

    g++ -shared   Z:/gdal-2.1.2/frmts/o/*.o Z:/gdal-2.1.2/gcore/*.o Z:/gdal-2.1.2/port/*.o Z:/gdal-2.1.2/alg/*.o Z:/gdal-2.1.2/apps/commonutils.o Z:/gdal-2.1.2/apps/gdalinfo_lib.o Z:/gdal-2.1.2/apps/gdal_translate_lib.o Z:/gdal-2.1.2/apps/gdalwarp_lib.o Z:/gdal-2.1.2/apps/ogr2ogr_lib.o Z:/gdal-2.1.2/apps/gdaldem_lib.o Z:/gdal-2.1.2/apps/nearblack_lib.o Z:/gdal-2.1.2/apps/gdal_grid_lib.o Z:/gdal-2.1.2/apps/gdal_rasterize_lib.o Z:/gdal-2.1.2/apps/gdalbuildvrt_lib.o Z:/gdal-2.1.2/ogr/ogrsf_frmts/o/*.o ./ogr/ogrgeometryfactory.o ./ogr/ogrpoint.o ./ogr/ogrcurve.o ./ogr/ogrlinestring.o ./ogr/ogrlinearring.o ./ogr/ogrpolygon.o ./ogr/ogrutils.o ./ogr/ogrgeometry.o ./ogr/ogrgeometrycollection.o ./ogr/ogrmultipolygon.o ./ogr/ogrsurface.o ./ogr/ogrmultipoint.o ./ogr/ogrmultilinestring.o ./ogr/ogrcircularstring.o ./ogr/ogrcompoundcurve.o ./ogr/ogrcurvepolygon.o ./ogr/ogrcurvecollection.o ./ogr/ogrmulticurve.o ./ogr/ogrmultisurface.o ./ogr/ogr_api.o ./ogr/ogrfeature.o ./ogr/ogrfeaturedefn.o ./ogr/ogrfeaturequery.o ./ogr/ogrfeaturestyle.o ./ogr/ogrfielddefn.o ./ogr/ogrspatialreference.o ./ogr/ogr_srsnode.o ./ogr/ogr_srs_proj4.o ./ogr/ogr_fromepsg.o ./ogr/ogrct.o ./ogr/ogr_opt.o ./ogr/ogr_srs_esri.o ./ogr/ogr_srs_pci.o ./ogr/ogr_srs_usgs.o ./ogr/ogr_srs_dict.o ./ogr/ogr_srs_panorama.o ./ogr/ogr_srs_ozi.o ./ogr/ogr_srs_erm.o ./ogr/swq.o ./ogr/swq_expr_node.o ./ogr/swq_parser.o ./ogr/swq_select.o ./ogr/swq_op_registrar.o ./ogr/swq_op_general.o ./ogr/ogr_srs_validate.o ./ogr/ogr_srs_xml.o ./ogr/ograssemblepolygon.o ./ogr/ogr2gmlgeometry.o ./ogr/gml2ogrgeometry.o ./ogr/ogr_expat.o ./ogr/ogrpgeogeometry.o ./ogr/ogrgeomediageometry.o ./ogr/ogr_geocoding.o ./ogr/osr_cs_wkt.o ./ogr/osr_cs_wkt_parser.o ./ogr/ogrgeomfielddefn.o ./ogr/ograpispy.o Z:/gdal-2.1.2/libgdal.a     -lodbc32 -lodbccp32 -lz -lpthread -lm  -lws2_32              -liconv      
            -o Z:/gdal-2.1.2/libgdal.so
    /usr/bin/sh: /c/mingw-w64/x86_64-7.1.0-win32-seh-rt_v5-rev0/mingw64/bin/g++: Argument list too long
    GNUmakefile:52: recipe for target 'Z:/gdal-2.1.2/libgdal.so' failed
    mingw32-make[1]: *** [Z:/gdal-2.1.2/libgdal.so] Error 126
    

    这里的错误与生成静态库原因一样,解决办法也与上面类似。
    可以看生成目标GDAL_LIB的动作,使用到的变量(依赖的.o和.a)与生成GDAL_SLIB是一致的。因为我们前面已经生成了basepart.aappspart.aogrpart1.aogrpart2.a,所以这里直接创建动态库即可(后缀名被我改为dll)。

    g++ -shared basepart.a appspart.a ogrpart1.a ogrpart2.a -lodbc32 -lodbccp32 -lz -lpthread -lm  -lws2_32 -liconv -o Z:/gdal-2.1.2/libgdal.dll
    

    注意:因为前面创建basepart.a等文件的时候,ar使用的参数是sr,才能成功。如果使用的参数是r,则会报下面错误:

    basepart.a: error adding symbols: Archive has no index; run ranlib to add one
    

    还要注意,上面链接的时候使用了-liconv参数,如果生成的Makefile中没有,则会报以下错误

    undefined reference to `_imp__GetACP@0
    
  • 相关阅读:
    Leetcode 15 3Sum
    Leetcode 383 Ransom Note
    用i个点组成高度为不超过j的二叉树的数量。
    配对问题 小于10 1.3.5
    字符矩阵的旋转 镜面对称 1.2.2
    字符串统计 连续的某个字符的数量 1.1.4
    USACO twofive 没理解
    1002 All Roads Lead to Rome
    USACO 5.5.1 求矩形并的周长
    USACO 5.5.2 字符串的最小表示法
  • 原文地址:https://www.cnblogs.com/oloroso/p/6856910.html
Copyright © 2011-2022 走看看