Subject: Re-ordering of shared libraries causing a crash

From: Mohammad Akhlaghi <[email-masked]>
Date: Wed, 12 Jun 2019 20:56:04 +0100

Dear Libtool maintainers,

A new user of Gnuastro recently reported a problem in building Gnuastro 
and after digging into the problem, I think its source lies with 
Libtool, so I thought of consulting you here.

Gnuastro first builds its library (libgnuastro), then goes onto building 
its separate programs (astarithmetic is the first one). The library 
needs to link with several dependency libraries. So when building the 
programs, `-lgnuastro' has to be linked first, then all the dependency 
libraries.

Recently we started using Gnulib's `AC_LIB_HAVE_LINKFLAGS' to check for 
the presence of libraries during the configure step. We append the 
results for all the dependency libraries into one variable 
(`CONFIG_LDADD') and use that when linking the individual programs. For 
example here you can see the Makefile.am for `astarithmetic':

https://git.savannah.gnu.org/cgit/gnuastro.git/tree/bin/arithmetic/Makefile.am

However, when there are multiple versions of a library in the search 
path, apparently `AC_LIB_HAVE_LINKFLAGS' doesn't actually add a `-l' 
option, but it returns the full path of the shared library. As a result, 
on my system, `CONFIG_LDADD' looks like this (notice them several `.so' 
files):

CONFIG_LDADD='-lgit2 -ltiff -llzma -ljpeg -lwcs -lcfitsio -lcurl -lz 
/usr/local/lib/libgsl.so /usr/local/lib/libgslcblas.so -lm -Wl,-rpath 
-Wl,/usr/local/lib -lm '

However, when we run `make' to build Gnuastro, Libtool apparently 
re-orders the libraries such that those without a `-l' come before those 
with it, you can see the relevant commands during `make' below (note how 
the order is preserved at first, but in the second command, the GSL 
`.so' files are all placed before those starting with `-l', in 
particular, before `libgnuastro.so'):

/bin/bash ../../libtool  --tag=CC   --mode=link gcc  -Wall -O3  -pthread 
-L\../../lib  -o astarithmetic main.o ui.o arithmetic.o operands.o 
../../bootstrapped/lib/libgnu.la -lgnuastro -lgit2 -ltiff -llzma -ljpeg 
-lwcs -lcfitsio -lcurl -lz /usr/local/lib/libgsl.so 
/usr/local/lib/libgslcblas.so -lm -Wl,-rpath -Wl,/usr/local/lib -lm
libtool: link: gcc -Wall -O3 -pthread -o .libs/astarithmetic main.o ui.o 
arithmetic.o operands.o /usr/local/lib/libgsl.so 
/usr/local/lib/libgslcblas.so -Wl,-rpath -Wl,/usr/local/lib  -L../../lib 
../../bootstrapped/lib/.libs/libgnu.a 
/home/id/gnuastro-0.9.55-9d7d/lib/.libs/libgnuastro.so -lgit2 -ltiff 
-llzma -ljpeg -lwcs -lcfitsio 
/usr/lib/x86_64-linux-gnu/libcurl-gnutls.so -lz -lm -pthread

On my system (Arch Linux, GCC 8.3.0) I also get a re-ordering, but it 
doesn't cause a problem. However, on the user's system (Ubuntu 18.04, 
GCC 7.4.0), it causes a linking error, complaining that there are 
undefined GSL symbols in `libgnuastro.so'.

To me it looks like the problem is in the re-ordering of the libraries. 
But I am a little confused why it complains on one system and doesn't 
complain on another.

Could you please guide me on the best way to solve this problem?

Thank you very much,
Mohammad