20

As soon as I upgraded my Ubuntu distro to 11.10, I started seeing strange linker behavior with gcc. I was able to fix the problem by moving my -l arguments to the end of the gcc command (my problem was similar to the one described in this thread, and the proposed solution worked for me...thanks!).

My question is...why did I have this problem only now? I've been developing and testing this code on OS X and Ubuntu for a while: I never knew that -l commands are supposed to go after your .c files, but even so, this never gave me problems before. I'm guessing it has more to do with the version of GCC than the Ubuntu release version.

Is this newer version simply enforcing this requirement more strictly than earlier versions?

Community
  • 1
  • 1
Daniel Standage
  • 8,136
  • 19
  • 69
  • 116
  • Can you see if [this post](https://stackoverflow.com/questions/38352092/why-do-i-get-dso-missing-error-even-when-the-linker-can-locate-the-library/45218113#45218113) answers your question? – yugr Oct 18 '17 at 12:00
  • 1
    Because of this: https://stackoverflow.com/questions/8207464/linking-math-library-in-gcc-4-6-1-ubuntu-11-10/8209036#8209036 – nos Jun 11 '18 at 10:08
  • 1
    Possible duplicate of [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/q/45135/608639) – jww Apr 26 '19 at 00:56
  • Possible duplicate of [Linking Math Library in GCC 4.6.1 (Ubuntu 11.10)](https://stackoverflow.com/questions/8207464/linking-math-library-in-gcc-4-6-1-ubuntu-11-10) – Lightness Races in Orbit May 10 '19 at 11:10

2 Answers2

5

With gcc but also other compilers (e.g. clang), the order of linker command arguments does matter. As a rule of thumb, I would use the following order when composing the linker command:

  1. Object files (*.o)
  2. Static libraries (*.a)
  3. Shared libraries (*.so)

The order of shared libraries does matter as well. If libfoo.so depends on libbar.so, you should list -lfoo before -lbar.

This can get quite complex if you don't know the exact dependencies. The following command on linux could help:

ldd /path/to/libfoo.so

This lists all shared libs on which libfoo.so depends.

As to your question why this problem popped up with your particular gcc version, it's hard to tell without knowing which libs your application requires. But if you apply the order like I described above, it should work for both older and newer gcc versions.

Hint: CMake, if used correctly, can handle all that dependency stuff for you...

Pat
  • 1,726
  • 11
  • 18
2

I suspect your problem was because Ubuntu v11.10's GCC v4.6 enabled -Wl,--as-needed by default, and that made the linker sensitive to the ordering of libraries on the command line.

  • Ubuntu v11.10 included GCC v4.6 as the default compiler [1].

  • Ubuntu v11.10's GCC enabled -Wl,--as-needed by default [2].

  • "The --as-needed option also makes the linker sensitive to the ordering of libraries on the command-line. You may need to move some libraries later in the command-line, so they come after other libraries or files that require symbols from them." [3]

[1] https://wiki.ubuntu.com/OneiricOcelot/ReleaseNotes#GCC_4.6_Toolchain

[2] https://wiki.ubuntu.com/ToolChain/CompilerFlags#A-Wl.2C--as-needed

[3] https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition

Daniel Le
  • 1,800
  • 1
  • 14
  • 23