Order of linked math libraries derails compilation
When I try to install eigensoft on a Ubuntu 14.04 system with gcc 4.8 by following the instructions in INSTALL.ubuntu, installation fails during make install. GCC seems to fail to link math.h functions; the relevant output:
gcc -I/home/chrism/eigensoft/src/smartinclude -g -lm -o convertf convertf.o mcio.o admutils.o /home/chrism/eigensoft/src/smartlib/nicklib.a -Wimplicit
mcio.o: In function `nextmesh':
mcio.c:(.text+0x18c4): undefined reference to `ceil'
...
I can fix the problem by rearranging the location of -lm. If I move -lm to the end of the command (specified on line 95 of the makefile), it completes successfully. I'm not sure I understand why exactly this hack is needed (and I needed to do this on a few of the subsequent gcc commands), but I believe it has something to do with the stricter linking behavior of gcc in newer versions.
The full details of my gcc setup:
$gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-4ubuntu8~14.04.2' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-4ubuntu8~14.04.2) ```
Probably caused by the fact that libraries should be specified in the order of their usage. See this Stack Overflow Issue GCC libm not working
For those who are currently stumbling upon this error, a quick'n'dirty one-liner fix could be:
awk -i inplace 'match($0,/ -lm/){print substr($0, 1, RSTART), substr($0, RSTART+RLENGTH+1), substr($0, RSTART+1, RLENGTH-1); next}{print}' ./Makefile