【问题标题】:Building GDAL with all libraries static使用所有静态库构建 GDAL
【发布时间】:2019-04-29 21:27:06
【问题描述】:

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

我已经下载了 GDAL 源代码 from here(2.3.2 最新稳定版本 - 2018 年 9 月),并按照构建说明 from here。由于我已经在我的 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

【问题讨论】:

  • 嗨@Rodrigo,我也遇到了同样的情况,你解决了吗?
  • @INElutTabile 我没有。但现在我开始了赏金,让我们看看这是否有帮助。
  • 你能澄清什么失败了吗?您是否收到链接错误或运行时错误?如果可以,请发布错误。
  • @bigh_29 上千行错误(仅针对gcc geofragc.cpp -l:libgdal.a),甚至不知道从哪里开始。最后一行是collect2: error: ld returned 1 exit status。我应该从上面的所有试验中显示所有错误吗?如果是这样,怎么做?如果没有,那又如何? (我什至不知道这是否是一个链接错误。)
  • @Rodrigo 不需要发布所有错误,但有一个会很好。 ld 失败确实表示链接器错误

标签: c++ gcc static-libraries gdal


【解决方案1】:

您可以使用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

然后用这个Makefile 打电话给make

希望对你有帮助。

【讨论】:

  • 从 gdal-config 选项列表中,你怎么知道你应该只使用这三个(cflags、libs 和 dep-libs),它们究竟是做什么的?
  • 有一个Linux工具pkg-config,它什么都不做,只返回一个文本字符串,对应于它的命令行选项。您可以使用man pkg-config 命令了解有关此工具的更多信息。 gdal-config 程序的设计类似于pkg-config。顺便说一句,GDAL 树也包含pkg-config 的配置文件,但它没有提供足够的链接信息(至少对于 2.3.2 版本)。
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 2018-10-05
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多