7

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.

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

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).

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?

EDIT

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
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
  • 1
    Hi @Rodrigo, I am in the very same situation, did you manage to solve this? – INElutTabile Jul 26 '19 at 07:27
  • 1
    @INElutTabile I didn't. But now I started a bounty, let's see if this helps. – Rodrigo Jul 29 '19 at 14:00
  • 1
    Can you clarify what fails? Do you get linking errors or runtime errors? Post the error if you can. – bigh_29 Jul 29 '19 at 14:03
  • 1
    @bigh_29 Thousands of lines of errors (just for `gcc geofragc.cpp -l:libgdal.a`), don't even know where to start from. Last line reads `collect2: error: ld returned 1 exit status`. Should I show all the errors, from all the trials above? If so, how? If not, what then? (I don't even know if that's a linking error.) – Rodrigo Jul 29 '19 at 20:00
  • 1
    @Rodrigo No need to post all the errors, but one would be good. `ld` failing does indicate a linker error – bigh_29 Jul 29 '19 at 21:13
  • @bigh_29 I tried "gcc geofragc.cpp -l:libgdal.a > error1.txt" but the error message came to the screen instead of going to error1.txt. Why?! – Rodrigo Jul 30 '19 at 20:11
  • @rodrigo You need to redirect `stderr` to that file. Try `gcc geofragc.cpp -l:libgdal.a 2> error1.txt` or `gcc geofragc.cpp -l:libgdal.a &> output.txt` See https://stackoverflow.com/questions/7526971/how-to-redirect-both-stdout-and-stderr-to-a-file – bigh_29 Jul 30 '19 at 21:44
  • @bigh_29 Thanks, it works. I don't have this `build` directory anymore, so I'll use the first two trials first. The first generated a 12 MB error file. The second is way smaller (386 bytes), and I'm going to append it to the question. Please tell me where should I share the first file, if needed. – Rodrigo Jul 30 '19 at 22:57
  • Regarding the last error, see [related question](https://stackoverflow.com/questions/2725255/create-statically-linked-binary-that-uses-getaddrinfo). – stgatilov Aug 04 '19 at 11:01

1 Answers1

5

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`

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

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

and just call make with this Makefile.

I hope, it'll help.

HEKTO
  • 3,876
  • 2
  • 24
  • 45
  • From the list of gdal-config options, how do you know that you should use only those three (cflags, libs and dep-libs), and what exactly do they do? – Rodrigo Aug 01 '19 at 23:21
  • There is a Linux tool `pkg-config`, which does nothing but returns a text string, corresponding to its command-line option. You can find out more about this tool using the `man pkg-config` command. The `gdal-config` program is designed similarly to the `pkg-config`. By the way, the GDAL tree contains a configuration file for the `pkg-config` as well, but it doesn't give enough information for linking (at least for the 2.3.2 version). – HEKTO Aug 02 '19 at 00:03