首页 > 解决方案 > 使用所有静态库构建 GDAL

问题描述

我想开发一个小程序来检查 shapefile 中的哪些多边形与给定的矩形相交。该程序将在网站中使用(使用 PHP 的exec()命令)。问题是,我的网络服务器无法安装 GDAL,原因我不知道。所以我无法链接到共享库。相反,我必须链接到静态库,但没有给出这些。

我已经从这里下载了 GDAL 源代码(2.3.2 最新稳定版本 - 2018 年 9 月),并按照这里的构建说明进行操作。由于我已经在我的 Debian 上安装了 GDAL,并且不想弄乱它,所以我按照“在非根目录中安装”的说明进行了操作,并对“一些警告”部分的最后一项进行了一些调整:

cd /home/rodrigo/Downloads/gdal232/gdal-2.3.2
mkdir build
./configure --prefix=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/ --without-ld-shared --disable-shared --enable-static
make
make install
export PATH=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/bin:$PATH
export LD_LIBRARY_PATH=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib:$LD_LIBRARY_PATH
export GDAL_DATA=/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/share/gdal
/usr/bin/gdalinfo --version
build/bin/gdalinfo --version

第一个/usr/bin/gdalinfo --version给出 2.1.2(以前安装的版本)。第二个,build/bin/gdalinfo --version给出 2.3.2(刚刚构建的版本)。

到目前为止,我的程序仅使用ogrsf_frmts.h头文件,它位于/usr/include/gdal//home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/include/目录中,具体取决于构建。没有ogrsf_frmts.a文件,只有一个libgdal.a. 这是我应该链接的文件吗?如果是这样,怎么做?到目前为止我已经尝试过:

gcc geofragc.cpp -l:libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l:/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -Wl,-Bstatic -l/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp /home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -l/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a
gcc geofragc.cpp -l:/home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/lib/libgdal.a

但没有任何效果。我错过了什么?

编辑

第二次试验 ( gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a) 给出以下错误:

/usr/bin/ld: cannot find -lgcc_s
/usr/lib/gcc/x86_64-linux-gnu/6/../../../../lib/libgdal.a(gdalclientserver.o): In function `GDALServerSpawnAsync()':
(.text+0x1f5e): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

标签: c++gccstatic-librariesgdal

解决方案


您可以使用该gdal-config程序获得正确的编译和链接选项。该程序是 GDAL 库的一部分,它有自己的选项:

hekto@ubuntu:~$ gdal-config --help
Usage: gdal-config [OPTIONS]
Options:
    [--prefix[=DIR]]
    [--libs]
    [--dep-libs]
    [--cflags]
    [--datadir]
    [--version]
    [--ogr-enabled]
    [--gnm-enabled]
    [--formats]

您必须确保该程序在您的搜索路径上,或者您可以创建一个别名 - 例如:

alias gdal-config='/home/rodrigo/Downloads/gdal232/gdal-2.3.2/bin/gdal-config'

现在您的编译和链接命令变为以下命令:

g++ `gdal-config --cflags` geofragc.cpp  `gdal-config --libs` `gdal-config --dep-libs`

您必须使用g++编译器来链接 C++ 构建的库。

另一种选择是Makefile使用这些行创建一个:

CXXFLAGS += ${shell gdal-config --cflags} 
LDLIBS += ${shell gdal-config --libs} 
LDLIBS += ${shell gdal-config --dep-libs} 
geofragc: geofragc.cpp

make然后用这个打电话Makefile

我希望,它会有所帮助。


推荐阅读