On Fri, Sep 03, 2021 at 12:24:39PM +0200, Piotr A. Dybczyński wrote: > Hi, > > in contrary to previous versions, now in Debian 11 with gcc-10: > > gcc aa.c -lm -o aa works, but > > gcc -lm aa.c -o aa does not work, saying: > > /usr/bin/ld: /tmp/ccWyhudO.o: in function `main': > aa.c:(.text+0x1f): undefined reference to `sqrt' > collect2: error: ld returned 1 exit status
If this *ever* worked in the past, it was dumb luck. Libraries need to be given after the objects that use them. That's how the linker operates. You can think of it this way: the linker scans through the list of object files and libraries in a single pass, left to right. In your non-working command, the first thing it encounters is -lm (math library). So it checks for any unresolved symbols that it currently has, and tries to match them against the math library. There aren't any unresolved symbols at this point (because no object files have been linked in yet), so it doesn't pull anything out of the math library. Next it encounters aa.o (implicitly compiled from aa.c), and it links in that object file, which may have some unresolved symbols. If there are any, the linker expects that it'll be given a library afterward, to look them up. But... there is no library given after that. It doesn't go *back* to look at the math library a second time.