天天看点

linux下编译GDAL3.x(集成Proj和Geos等)

目录

  • 1、准备工作
  • 2、生成 Makefile
    • 1、报错 "checking for sqlite3_open in -lsqlite3 ... no"
    • 2、 checking for proj_create_from_wkt in -lproj 未通过
    • 3、checking for H5Fopen in -lhdf5... no
    • 4、configure 完成,输出 geos 等为 no
  • 3、编译
    • 1、编译错误 libproj.a: error adding symbols: Bad value
    • 2、找不到

      curl_xxx

      GEOSxxxx

      等定义
    • 3、 undefined reference to `GDALVersionInfo'
    • 4、hidden symbol `curl_multi_info_read' in ...
  • 参考:

  • 1、安装

    gcc

    vcpkg

    等。
  • 2、下载最新的 GDAL 源码。
  • 3、使用

    vcpkg

    安装第三方库。
    ./vcpkg install tiff
    install sqlite3[tool]
    ./vcpkg install geos
    ./vcpkg install curl
    ./vcpkg install liblzma
    ./vcpkg install hdf5
    ./vcpkg install libwebp
    ./vcpkg install zstd
    ./vcpkg install openjpeg
               

使用

vcpkg

编译出来的都是静态库,如果机器上默认的库目录已经有了对应的版本,下面指定链接库的时候就不要使用

-Lxxxx -lyyy

的方式,而应该直接使用

/xx/vcpkg/installed/x64-linux/lib/libxxxx.a

库完整路径的方式,否则可能链接的不是你想要的那一个。

进入 GDAL 源码目录下,使用

configure

脚本生成

Makefile

文件。

LIBRARY_PATH=/home/x/code/vcpkg/installed/x64-linux/lib \
 CPPFLAGS="-I/home/x/code/vcpkg/installed/x64-linux/include" \
 LDFLAGS="-s -L/home/x/code/vcpkg/installed/x64-linux/lib" \
 LIBS="-lpthread -ldl" HDF5_LIBS="-hdf5 -lszip" \
 ./configure \
 --prefix=/home/x/code/output --disable-rpath \
 --with-libtiff=internal --with-libz=internal \
 --with-curl=/home/x/code/vcpkg/installed/x64-linux \
 --with-sqlite3=/home/x/code/vcpkg/installed/x64-linux \
 --with-proj=/home/x/code/vcpkg/installed/x64-linux \
 --with-proj-extra-lib-for-test="-ltiff -ljpeg -llzma -lz" \
 --with-liblzma=/home/x/code/vcpkg/installed/x64-linux \
 --with-zstd=yes --with-png=internal --with-geotiff=internal \
 --with-jpeg=internal \
 --with-gif=internal \
 --with-hdf5=/home/x/code/vcpkg/installed/x64-linux \
 --with-openjpeg=yes --with-geos=yes \
 --with-webp=/home/x/code/vcpkg/installed/x64-linux --with-qhull=internal
           

通过查看

config.log

发现

checking for sqlite3_open in -lsqlite3

的时候没有链接

pthread

dl

库(找不到

pthread_xxxxx

以及

dlcopen

等函数的定义) 导致的错误。

执行

configure

的时候添加环境变量

LIBS="-lpthread -ldl"

来传入额外的库。

查看

configure

文件,找到

24944

$as_echo_n "checking for sqlite3_open in -lsqlite3... " >&6; }

发现上面有一行

LIBS=""

这样导致传入的

LIBS

被清空,注释掉这一行继续。

还是查看

config.log

文件,找到相关行的输出,发现是没有链接

tiff

(以及

jpeg

lzma

等)库,导致的找不到相关函数的定义。

configure

脚本的时候,添加

--with-proj-extra-lib-for-test="-ltiff -ljpeg -llzma -lz"

选项即可。

完整报错信息如下:

checking for H5Fopen in -lhdf5... no
 
 configure:33439: error: HDF5 support requested with arg /home/x/code/vcpkg/installed/x64-linux, but no hdf5 lib found
 
 configure:33395: checking for H5Fopen in -lhdf5
configure:33420: gcc -o conftest -DHAVE_AVX_AT_COMPILE_TIME -DHAVE_SSSE3_AT_COMPILE_TIME -DHAVE_SSE_AT_COMPILE_TIME -g -O2 -I/home/x/code/vcpkg/installed/x64-linux/include -s -L/home/x/code/vcpkg/installed/x64-linux/lib conftest.c -lhdf5 -lhdf5 -L/home/x/code/vcpkg/installed/x64-linux/lib -L/home/x/code/vcpkg/installed/x64-linux -L/home/x/code/vcpkg/installed/x64-linux/lib -ljpeg -lzstd -L/home/x/code/vcpkg/installed/x64-linux/lib -lproj -ltiff -ljpeg -llzma -lz -L/home/x/code/vcpkg/installed/x64-linux/lib -lsqlite3 -lpthread -lm -lrt -ldl -lpthread -ldl -lhdf5 >&5
/home/x/code/vcpkg/installed/x64-linux/lib/libhdf5.a(H5Z.c.o): In function `H5Z__init_package':
H5Z.c:(.text+0x6a1): undefined reference to `SZ_encoder_enabled'
/home/x/code/vcpkg/installed/x64-linux/lib/libhdf5.a(H5Zszip.c.o): In function `H5Z_filter_szip':
H5Zszip.c:(.text+0xe4): undefined reference to `SZ_BufftoBuffDecompress'
H5Zszip.c:(.text+0x195): undefined reference to `SZ_BufftoBuffCompress'
           

这些找不到的,需要链接

libszip

。于是添加

HDF5_LIBS

选项,结果还是不行。

找打

configure

33401

行(大概这个位置,搜索HDF5就是)

LIBS="-lhdf5 $LIBS"

(搜索

but no hdf5 lib found

往上面一点点找),修改为

LIBS="-lhdf5 -lszip $LIBS"

,重新执行。

这里是没有识别出

geos

等库,导致没有引用到。这里的原因也不好找,直接打开

configure

脚本文件,找到对

GEOS

库进行检查的位置,在后面添加下面代码

HAVE_GEOS="yes"
HAVE_GEOS_RESULT="yes"
GEOS_LIBS="-lgeos -lgeos_c ${LIBS}"
           

其它的库(如

curl

等)也可以进行同样的操作。

LZMA		LIBLZMA_SETTING="yes"


curl		CURL_SETTING=yes
			CURL_INC=
			CURL_LIB="-lcurl $LIBS"
			这里有可能编译的gdal链接是系统库目录下 libcurl.so 而不是 vcpkg 安装的 libcurl.a
			可以直接打开 GDALmake.opt 文件,将里面对应的位置 $(CURL_LIB) 修改为 
			    /home/x/code/vcpkg/installed/x64-linux/lib/libcurl.a
			
openjepg	HAVE_OPENJPEG="yes"
			OPENJPEG_LIBS="-lopenjp2 ${LIBS}"
			OPT_GDAL_FORMATS="openjpeg $OPT_GDAL_FORMATS"
           

上面正常生成

Makefile

之后,执行

make -j4

进行编译。

这里详细的报错信息没有记录下来,但是不影响这里记录解决办法。

GCC 提示

recompile with -fPIC

,需要对

libproj

-fPIC

选项进行重新编译。这里我是自己编译的

libproj

,直接在执行

cmake

生成

Makefile

的时候,添加

-DCMAKE_C_FLAGS=-fPIC -DCMAKE_CXX_FLAGS=-fPIC

选项重新生成,重新编译之后替换即可。

curl_xxx

GEOSxxxx

部分报错信息如下:

/bin/sh /home/x/code/gdal/gdal/libtool --mode=link --silent g++ -s -L/home/x/code/vcpkg/installed/x64-linux/lib gdaladdo.lo  /home/x/code/gdal/gdal/libgdal.la -o gdaladdo
/home/x/code/gdal/gdal/.libs/libgdal.so: undefined reference to `curl_multi_info_read'
/home/x/code/gdal/gdal/.libs/libgdal.so: undefined reference to `GEOSOverlaps_r'
/home/x/code/gdal/gdal/.libs/libgdal.so: undefined reference to `GEOSTouches_r'
/home/x/code/gdal/gdal/.libs/libgdal.so: undefined reference to `curl_mime_name'
/home/x/code/gdal/gdal/.libs/libgdal.so: undefined reference to `GEOSPolygonize_r'
/home/x/code/gdal/gdal/.libs/libgdal.so: undefined reference to `GEOSGetCentroid_r'
           

这是因为没有链接

libgeos_c.a

libgeos.a

libcurl.a

等库的原因。

打开

apps/GNUMakefile

文件,在前面添加一行

CONFIG_LIBS =   -L/home/x/code/vcpkg/installed/x64-linux/lib -lgeos_c -lgeos -lcurl \
                -lssl -lcrypto -lszip -pthread
           

这里也可以通过修改

GDALmake.opt

文件来达到目的,我修改过的

LIBS

变量如下:、

LIBS    =       $(SDE_LIB) -lcrypto -L/home/x/code/vcpkg/installed/x64-linux/lib -lwebp \
                -L/home/x/code/vcpkg/installed/x64-linux/lib -lhdf5 -lopenjp2 \
                -lzstd -L/home/x/code/vcpkg/installed/x64-linux/lib \
                -lproj -ltiff -ljpeg -llzma -lz \
                /home/x/code/vcpkg/installed/x64-linux/lib/libgeos_c.a \
                /home/x/code/vcpkg/installed/x64-linux/lib/libgeos.a \
                /home/x/code/vcpkg/installed/x64-linux/lib/libcurl.a \
                /home/x/code/vcpkg/installed/x64-linux/lib/libssl.a \
                /home/x/code/vcpkg/installed/x64-linux/lib/libcrypto.a \
                /home/x/code/vcpkg/installed/x64-linux/lib/libszip.a \
                /home/x/code/vcpkg/installed/x64-linux/lib/libsqlite3.a \
                $(KAK_LIBS) $(DWG_LIBS) \
                $(MRSID_LIBS) $(MRSID_LIDAR_LIBS) $(ECW_LIBS) $(INGRES_LIB) \
                $(PCIDSK_LIB) $(RASDAMAN_LIB) $(SOSI_LIB) \
                $(OPENCL_LIB) $(JVM_LIB) $(LIBICONV) $(FGDB_LIB) $(LIBXML2_LIB) \
                $(MONGODB_LIB)  $(MONGOCXXV3_LIBS) $(JNI_LIB) $(HDFS_LIB) \
                -lpthread -lm -lrt -ldl -lpthread -ldl 
           

部分指定了完整路径的库,是因为本地系统路径下有对应的

.so

,所以导致默认先去链接了

.so

文件,导致编译出来的

libgdal.so

会有一些符号是为包含在其中的(需要从相关库导入),所以直接指定静态库完整路径,将这些符号的定义都链接到输出的

libgdal.so

里面去。

做上面修改后,继续执行

make

,再次报错,部分报错信息如下:

/bin/sh /home/x/code/gdal/gdal/libtool --mode=link --silent g++ -s -L/home/x/code/vcpkg/installed/x64-linux/lib gdaladdo.lo  -L/home/x/code/vcpkg/installed/x64-linux/lib -lgeos_c -lgeos -lcurl -lssl -lcrypto -lszip -pthread -o gdaladdo
.libs/gdaladdo.o: In function `main':
/home/x/code/gdal/gdal/apps/gdaladdo.cpp:117: undefined reference to `GDALVersionInfo'
/home/x/code/gdal/gdal/apps/gdaladdo.cpp:126: undefined reference to `GDALAllRegister'
/home/x/code/gdal/gdal/apps/gdaladdo.cpp:128: undefined reference to `GDALGeneralCmdLineProcessor'
/home/x/code/gdal/gdal/apps/gdaladdo.cpp:139: undefined reference to `GDALTermProgress'
/home/x/code/gdal/gdal/apps/gdaladdo.cpp:195: undefined reference to `CPLRealloc'
/home/x/code/gdal/gdal/apps/gdaladdo.cpp:244: undefined reference to `CPLPushErrorHandler'
           

这里是因为没有链接

libgdal

的原因,修改上面添加的那句为

CONFIG_LIBS =   -L$(GDAL_ROOT)/.libs -lgdal -L/home/x/code/vcpkg/installed/x64-linux/lib \
                -lgeos_c -lgeos -lcurl \
                -lssl -lcrypto -lszip -pthread
           

报错信息如下:

.libs/gdalinfo: hidden symbol `curl_multi_info_read' in /home/x/code/vcpkg/installed/x64-linux/lib/libcurl.a(multi.c.o) is referenced by DSO
           

这个问题是因为

libcurl.a

里面的

curl_multi_info_read

是个隐藏符号(符号可见性问题),重新编译下

curl

即可。

修改

vcpkg/buildtrees/curl/src/url-7_68_0-cd669cc759/include/curl/curl.h

文件,在 定义

CURL_EXTERN

相关代码的后面,加上

#undef CURL_EXTERN
#define CURL_EXTERN __attribute__ ((visibility("default"))) 
           

然后在

vcpkg/buildtrees/curl/x64-linux-rel

vcpkg/downloads/tools/ninja-1.10.0-linux/ninja all

编译完成后,把 当前

lib

目录下的

libcurl.a

覆盖

vcpkg/installed/x64-linux/lib/libcurl.a

然后重新执行

make

编译即可。

  • 如何解决 error adding symbols: Bad value 问题
  • Clang 11 documentation/LTO Visibility
  • Symbol Visibility Macros
  • (attribute((visibility("visibility_type"))) variable attribute)[http://www.keil.com/support/man/docs/armcc/armcc_chr1359124983511.htm]
  • osgeo_compile.sh
  • gdal/.github/workflows/ubuntu_20.04/build.sh