~emersion/mrsh-dev

mrsh: Move $(LIBS) to end of compile command v1 APPLIED

Andrew Paon: 1
 Move $(LIBS) to end of compile command

 1 files changed, 2 insertions(+), 2 deletions(-)
#844621 alpine.yml success
#844622 archlinux.yml success
#844623 freebsd.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/patches/35307/mbox | git am -3
Learn more about email & git

[PATCH mrsh] Move $(LIBS) to end of compile command Export this patch

Without this change, when I run `make` it fails with this error (edited
for clarity):

```
ld: frontend/readline.o: in function `sigint_handler':
mrsh/frontend/readline.c:33: undefined reference to `rl_on_new_line'
ld: mrsh/frontend/readline.c:34: undefined reference to `rl_replace_line'
ld: mrsh/frontend/readline.c:35: undefined reference to `rl_redisplay'
ld: frontend/readline.o: in function `interactive_init':
ld: mrsh/frontend/readline.c:53: undefined reference to `read_history'
ld: frontend/readline.o: in function `interactive_next':
ld: mrsh/frontend/readline.c:74: undefined reference to `add_history'
ld: mrsh/frontend/readline.c:76: undefined reference to `write_history'
collect2: error: ld returned 1 exit status
make: *** [Makefile:61: mrsh] Error 1
```

It turns out that the placement of `-l` in the command is significant
[1]; it should come after the object files that depend on the library.

> It makes a difference where in the command you write this option; the
> linker searches and processes libraries and object files in the order
> they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after
> file foo.o but before bar.o. If bar.o refers to functions in ‘z’,
> those  functions may not be loaded.

Moving $(LIBS) to the end of the command (right next to -lmrsh) fixes
the error.

Tested using gcc 12.2.0 and clang 15.0.0

[1] https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
---
 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index ad9ada2..1730c5a 100644
--- a/Makefile
+++ b/Makefile
@@ -58,11 +58,11 @@ $(OUTDIR)/mrsh.pc:

mrsh: $(OUTDIR)/libmrsh.a $(mrsh_objects)
	@printf 'CCLD\t$@\n'
	@$(CC) -o $@ $(LDFLAGS) $(LIBS) $(mrsh_objects) -L$(OUTDIR) -lmrsh
	@$(CC) -o $@ $(LDFLAGS) $(mrsh_objects) -L$(OUTDIR) -lmrsh $(LIBS)

highlight: $(OUTDIR)/libmrsh.a $(highlight_objects)
	@printf 'CCLD\t$@\n'
	@$(CC) -o $@ $(LDFLAGS) $(LIBS) $(highlight_objects) -L$(OUTDIR) -lmrsh
	@$(CC) -o $@ $(LDFLAGS) $(highlight_objects) -L$(OUTDIR) -lmrsh $(LIBS)

check: mrsh $(tests)
	@for t in $(tests); do \
--
2.37.3
Pushed, thanks!
mrsh/patches: SUCCESS in 1m8s

[Move $(LIBS) to end of compile command][0] from [Andrew Paon][1]

[0]: https://lists.sr.ht/~emersion/mrsh-dev/patches/35307
[1]: mailto:andrew@paon.wtf

✓ #844623 SUCCESS mrsh/patches/freebsd.yml   https://builds.sr.ht/~emersion/job/844623
✓ #844622 SUCCESS mrsh/patches/archlinux.yml https://builds.sr.ht/~emersion/job/844622
✓ #844621 SUCCESS mrsh/patches/alpine.yml    https://builds.sr.ht/~emersion/job/844621