John Collins
John Collins

Reputation: 31

Same versions of Linux, CC, LD have different link order rules

I have a desktop PC and a laptop PC both running identical Linuxes (Ubuntu 22.04.4 LTS) but the desktop has few extra bits and pieces loaded, I would have thought irrelevant, both are running gcc-12 as the "main" one and the same version of "ld" - 2.38.

The contents of /etc/ld-conf.d are the same in each case

Context is I'm working on what is intended to be a C shared library currently with 2 modules "symbols.o" and "stringvec.o". I've stuck a "main" routine on the end of "symbols.c" whilst I debug those modules, so for the purposes of the question I'm asking about a progam.

The makefile has in it:

CFLAGS=-g -I incl
LDFLAGS=-lm

all:    symbols

symbols: stringvec.o symbols.o

On both machines "make" gives

cc -g -I incl   -c -o symbols.o symbols.c
cc -g -I incl   -c -o stringvec.o stringvec.c
cc -lm  symbols.o stringvec.o   -o symbols

On the desktop the final step returns to the command prompt with "symbols" built but on the laptop it moans about symbols from "-lm" missing.

/usr/bin/ld: symbols.o: in function `getstringvalue':
/home/jmc/src/products/commonlib/symbols.c:176: undefined reference to `nearbyint'
/usr/bin/ld: symbols.o: in function `eval_tree':
/home/jmc/src/products/commonlib/symbols.c:433: undefined reference to `pow'
/usr/bin/ld: /home/jmc/src/products/commonlib/symbols.c:439: undefined reference to `fetestexcept'
/usr/bin/ld: /home/jmc/src/products/commonlib/symbols.c:441: undefined reference to `feclearexcept'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: symbols] Error 1

I can get round this easily by putting in the last part of the Makefile

$(CC) -o symbols stringvec.o symbols.o $(LDFLAGS)

But why the difference? Why is --as-needed turned on in one case but not the other? And why is the default make rule "wrong" as it were?

This only came up because I'd worked on it on the Desktop and thought I'd debugged it all and thought I'd just copy it to the laptop in case the urge to further fiddle with it over the next few days whilst I'm away was too strong.

I have tried -Wl,--as-needed in LDFLAGS but that doesn't seem to help.

I did notice slightly different output with -t option to LD.

Upvotes: 0

Views: 99

Answers (0)

Related Questions