Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #239614

Re: gcc-10: options order important?

From Greg Wooledge <greg@wooledge.org>
Newsgroups linux.debian.user
Subject Re: gcc-10: options order important?
Date 2021-09-03 13:40 +0200
Message-ID <CTfI5-4Ib-3@gated-at.bofh.it> (permalink)
References <CTeCm-42y-11@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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.

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

gcc-10: options order important? "Piotr A. Dybczyński" <piotr@dybczynski.pl> - 2021-09-03 12:30 +0200
  Re: gcc-10: options order important? Greg Wooledge <greg@wooledge.org> - 2021-09-03 13:40 +0200
  Re: gcc-10: options order important? Tixy <tixy@yxit.co.uk> - 2021-09-03 15:20 +0200
    Re: gcc-10: options order important? Greg Wooledge <greg@wooledge.org> - 2021-09-03 19:00 +0200
      Re: gcc-10: options order important? <tomas@tuxteam.de> - 2021-09-03 20:50 +0200
  Re: gcc-10: options order important? Tixy <tixy@yxit.co.uk> - 2021-09-03 15:20 +0200
  Re: gcc-10: options order important? Sven Joachim <svenjoac@gmx.de> - 2021-09-03 21:00 +0200

csiph-web