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