使用所有库静态构建GDAL [英] Building GDAL with all libraries static

查看:80
本文介绍了使用所有库静态构建GDAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to develop a small program that checks which polygons from a shapefile intersect a given rectangle. This program is to be used in a website (with PHP's exec() command). The problem is, my webserver cannot install GDAL, for reasons unknown to me. So I can't link to the shared libraries. Instead, I must link to static libraries, but these aren't given.

我已经从此处下载了GDAL源代码 (2.3.2最新稳定版)版本-2018年9月),并按照构建说明从此处进行操作.由于我已经在GDAL上运行了Debian,并且不想弄乱它,因此我遵循了在非根目录下安装"的说明,并对某些注意事项"部分中的最后一项进行了一些调整:

I've downloaded the GDAL source code from here (2.3.2 Latest Stable Release - September 2018), and followed the build instructions from here. Since I already have GDAL working on my Debian, and don't want to mess with it, I followed the "Install in non-root directory" instructions, with some adjusts from the last item in the "Some caveats" section:

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(刚刚构建的版本).

The first /usr/bin/gdalinfo --version gives 2.1.2 (the previous installed version). The second, build/bin/gdalinfo --version, gives 2.3.2 (the version just built).

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

By now, my program only uses the ogrsf_frmts.h header, which is in /usr/include/gdal/ or /home/rodrigo/Downloads/gdal232/gdal-2.3.2/build/include/ directory, depending on the build. There's no ogrsf_frmts.a file, but only a libgdal.a. Is this the file I should be linking against? If so, how? I've tried so far:

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

但没有任何效果.我想念什么?

but nothing works. What am I missing?

编辑

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

The second trial (gcc geofragc.cpp -Wl,-Bstatic -l:libgdal.a) is giving the following error:

/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库的一部分,它具有自己的选项:

You can use the gdal-config program to get correct options for compilation and linking. This program is a part of the GDAL library and it has its own options:

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

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

You have to make sure this program is on your search path, or you can create an alias - for example:

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

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

Now your compilation and linking command becomes the following one:

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

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

You have to use the g++ compiler to link with C++-built libraries.

另一种选择是用以下几行创建一个 Makefile :

Another option is to create a Makefile with these lines:

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

,然后使用此 Makefile 调用 make .

我希望会有所帮助.

这篇关于使用所有库静态构建GDAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆