LDLIBS in portable makefiles

Joe Nelson joe at begriffs.com
Sun Apr 19 22:11:29 UTC 2020

Hi Chris, I notice that your makefile article [1] uses the macro name
LDLIBS for -l options like -lm. That's reminiscent of GNU make's .c
suffix rule [2]:


		$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

Do you know why they separate the -L flags in LDFLAGS vs -l flags in
LDLIBS? I realize that GCC's linker has trouble if the -l flags precede
the source file(s), but my question is why don't they put all the flags
(both -L and -l) together into LDFLAGS and put that macro at the end?

For instance, using this suffix rule:

		$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

POSIX make [3] doesn't recognize LDLIBS in its default rules. And its .c
suffix rule puts the LDFLAGS before the source name:

		$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

The clang compiler is fine with the -l flags going before the source
file actually, which makes it work with the POSIX defaults.

Do you know the background behind GNU's divergence?

1: https://nullprogram.com/blog/2017/08/20/
2: https://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html
3: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html#tag_20_76_13_09

More information about the Friends mailing list