c++ - 使用所有静态库构建 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
解决方案
您可以使用该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
。
我希望,它会有所帮助。
推荐阅读
- c# - 调试引发异常的属性时显示异常消息
- scala - 未生成反向路由
- html - 在css中为不同的视频添加视频源
- python - Python:使用不同大小的数据框根据日期时间条件创建新列
- cypress - Cypress - 带有请求有效负载的 API 调用 - 如何在代码中处理有效负载
- coq - 对于 Coq 中的异构列表,是否可以证明与 Forall_inv 等效?
- c# - Oracle c# 大容量复制Datatable
- android - 未处理的异常:SocketException:操作系统错误:连接被拒绝,errno = 111,地址 = 127.0.0.1,端口 = 40712
- c++ - 在每个元音上添加字符
- ios - 当我们尝试使用 Appium 脚本导航到 iPhone safari 浏览器中的框架时,获取异常阻止了一个具有原点的框架