1

I am building an executable (foo.exe let's call it) on RHEL with gcc 6.2. It links against a few third-party libraries, libzzdesign.so, libyydesign.so. Yydesign uses dlopen/dlclose/dlerror. I would expect this command-line to work:
g++ -Wall -fcheck-new -fno-strict-aliasing -msse2 -fno-omit-frame-pointer -pthread -O3 -Wl,--export-dynamic -o foo.exe foo.o -L/path/to/zzdesign -Wl,-rpath=/path/to/zzdesign -lzzdesign -L/path/to/yydesign -Wl,-rpath=/path/to/yydesign -lyydesign -ldl
(I'm listing all the options used in case it matters)

It produces the errors,
/path/to/yydesign/libyydesign.so: undefined reference to 'dlclose'
/path/to/yydesign/libyydesign.so: undefined reference to 'dlerror'

If I change the command line to put -ldl before -lyydesign:
g++ -Wall -fcheck-new -fno-strict-aliasing -msse2 -fno-omit-frame-pointer -pthread -O3 -Wl,--export-dynamic -o foo.exe foo.o -L/path/to/zzdesign -Wl,-rpath=/path/to/zzdesign -lzzdesign -L/path/to/yydesign -Wl,-rpath=/path/to/yydesign -ldl -lyydesign
... it works without error.

This is the opposite of everything I thought I knew about order of libraries on the command line when linking.

  1. Why does -ldl have to come before -lyydesign?
  2. Other than dumb luck to stumble across this solution, how could I troubleshoot the original error to understand what's going on?
  3. And since changing the build system to move -ldl first in all the places it's needed is kind of a pain, is there a way I can avoid having to put -ldl first?
MaddHatter
  • 63
  • 1
  • 3
  • If don't want to correct the dependency ordering, use `--start-group [ libs ] --end-group`. – Mansoor Sep 17 '20 at 09:22
  • 3
    Is it generally the case that a dependee should appear before the depender when linking? I've always heard it should be depender followed by dependee (for example, [here](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc).) – MaddHatter Sep 17 '20 at 09:29
  • This doesn't look like a thing that could possibly happen in this here universe. – n. m. could be an AI Sep 17 '20 at 09:39
  • It's repeatable -- both the error in the first form and success in the second. Any suggestions to further track down what's going on then? – MaddHatter Sep 17 '20 at 09:46
  • Try to minimize it to just `-lyydesign -ldl`, without zzdesign etc. Also try with `-Wl,--no-as-needed -ldl`. – rustyx Sep 17 '20 at 09:53
  • I cannot reproduce the undefined reference error with *any* position of `-ldl` on the command line, from the very first to the very last. – n. m. could be an AI Sep 17 '20 at 09:57
  • As usual, try to produce a [mcve]. First reduce `foo.c` to a single function call to `libyydesign` and get rid of `libzzdesign`. Second, reduce `libyydesign` to a single function that calls `dlopen`, `dlerror`, `dlclose`. Third, post it all here together with build commands *as is*, without editing a single letter. – n. m. could be an AI Sep 17 '20 at 10:04
  • 1
    I suspect RHEL and gcc 6 (and whatever compatibility shims redhat might have done) are an important part of the puzzle. It's not an issue with gcc 4. I'll need to make a simplified test case in order to test other OSes/versions. – MaddHatter Sep 17 '20 at 10:10
  • I was afraid of this... the simplest test case doesn't reproduce the issue, so I'm back to question #2: how can I debug what's happening here? nm for both simple test case and actual library demonstrating this problem show dladdr, dlclose, dlerror, dlopen, and dlsym as undefined. – MaddHatter Sep 17 '20 at 16:01
  • If creating a reproducible case from scratch doesn't work, then start removing code from the actual program bit by bit until you are left with the smallest amount of code possible. – n. m. could be an AI Sep 20 '20 at 11:21
  • Then what? I have a smaller example that still doesn't work and that I still have no way to troubleshoot. – MaddHatter Sep 21 '20 at 17:53

1 Answers1

-2

Order of libs for LD does matter. Lib yydesign use dlclose and dlerror that's why lib dl need to be passed before yydesign.

Login
  • 105
  • 1