0

I'm bringing an Ubuntu 14.04 OS up to a build standard. The goal is for it to replace an existing SUSE SLED 11 SP2 builder. But I've hit an error I find difficult to explain.

The build command which produces the error is

g++
-o
out/linux-rel/bin/PrtReporter
-fstack-protector
-m32
-Wl,-rpath,/local/out/linux-rel/bin
-l:libz.so.1.2.8
out/linux-rel/obj-static/prt/CrashReporter.o
out/linux-rel/obj-static/prt/LibArchiveWrapper.o
out/linux-rel/obj-static/utilities/RTSConfigUtilities.o
-Lout/linux-rel/bin
-Lout/linux-rel/bin
out/linux-rel/bin/libcurl.so
out/linux-rel/bin/libcurl.a
out/linux-rel/bin/libarchive.a
-lpthread
-lrt
-lxml2

On Ubuntu this results in a link error. g++ cannot find the symbols from libz.

To fix this I can simply place -l:libz.so.1.2.8 after out/linux-rel/bin/libarchive.a. This 'looks' correct as it allows libarchive.a to find the libz symbols. i.e. Which I believe should be listed after the library that depends upon them.

But I don't know why this is necessary on Ubuntu. The same command works fine on SLED.

Any ideas?

Shane Gannon
  • 6,770
  • 7
  • 41
  • 64
  • http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc – n. m. could be an AI Aug 11 '16 at 16:46
  • Useful link. But it does not explain the difference between Ubuntu and SLED for me. – Shane Gannon Aug 11 '16 at 17:01
  • Ah - I spoke too soon. A link which I found in the above helped answer my question. Namely http://stackoverflow.com/questions/7826448/linking-libraries-with-gcc-order-of-arguments - which lead to https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition. So this is Ubuntu specific changes made to ld? Unusual. – Shane Gannon Aug 11 '16 at 17:04

1 Answers1

1

g++ cannot find the symbols from libz.

You have libz in the wrong place on the link line, and Ubuntu configured their gcc to pass --as-needed to the linker by default.

You can run readelf -d PrtReporter | grep libz and verify that libz.so is missing (i.e. the loader will not even try to load it).

To fix, add -Wl,--no-as-needed or (better) move libz to correct place on the link line.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • FYI: This flag can be set on g++ using -Xlinker --no-as-needed. -Xlinker allows you to pass commands onto ld. – Shane Gannon Aug 17 '16 at 15:09
  • @ShaneGannon You should use `-Wl,--no-as-needed` instead of `-Xlinker`. The *proper* fix is still to move `libz` into correct position on the link line. – Employed Russian Aug 17 '16 at 15:23
  • You are correct. But I'm taking baby steps. I want to get the current build working on Ubuntu without much modification. Once that's done we can fix this obvious flaw. PS: I assume -Wl and -Xlinker are equivalent. – Shane Gannon Aug 18 '16 at 13:04
  • @ShaneGannon The `-Wl` and `-Xlinker` are somewhat equivalent, but `-Wl` is supported by many compilers (`-Xlinkder` is GCC-specific). – Employed Russian Aug 18 '16 at 14:12