mingw 構建 gdal 2.1.2

來源:互聯網
上載者:User

標籤:環境   動態   core   str   ++   tip   一個   prefix   share   

前兩日有人在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的路徑匯入環境變數PATHexport 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 longGNUmakefile:47: recipe for target ‘Z:/gdal-2.1.2/libgdal.a‘ failedmingw32-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 產生這四個靜態庫的參數是 srar 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.oar 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 longGNUmakefile:52: recipe for target ‘Z:/gdal-2.1.2/libgdal.so‘ failedmingw32-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 `[email protected]

mingw 構建 gdal 2.1.2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.