jedisct1 / libsodium-doc

Gitbook documentation for libsodium
https://libsodium.org
ISC License
166 stars 159 forks source link

Change LDFLAGS to LDLIBS #179

Closed realconebob closed 1 month ago

realconebob commented 1 month ago

Setting LDFLAGS to $(pkg-config --libs libsodium) in a makefile results in a linker error, whereas setting LDLIBS to the same value works fine

Example makefiles:

# Bad, linker error

CC = gcc
CFLAGS = $$(pkg-config --cflags libsodium)
LDFLAGS = $$(pkg-config --libs libsodium)

.PHONY: all, clean

all: client
clean:
    rm -rvf client client.o

client: client.o ../shared/shared.o
client.o: client.c client.h

# OUTPUT:
# gcc $(pkg-config --libs libsodium)  client.o ../shared/shared.o   -o client
# /usr/bin/ld: ../shared/shared.o: in function `testfunction':
# shared.c:(.text+0x3d9): undefined reference to `sodium_init'
# /usr/bin/ld: shared.c:(.text+0x433): undefined reference to `randombytes_uniform'
# collect2: error: ld returned 1 exit status
# make: *** [<builtin>: client] Error 1
# Good, no linker error

CC = gcc
CFLAGS = $$(pkg-config --cflags libsodium)
LDLIBS = $$(pkg-config --libs libsodium)

.PHONY: all, clean

all: client
clean:
    rm -rvf client client.o

client: client.o ../shared/shared.o
client.o: client.c client.h

# OUTPUT:
# gcc   client.o ../shared/shared.o  $(pkg-config --libs libsodium) -o client

Note: This is my first contribution to a reasonably large public project. Any constructive criticism is appreciated

jedisct1 commented 1 month ago

Hi,

The GNU make manual says that -l parameters should go into LDLIBS but the -L parameters should go to LDFLAGS.

pkg-config has the --libs-only-l and --libs-only-L options to solve this.

So maybe this should be:

LDFLAGS += $$(pkg-config --libs-only-L libsodium)
LDLIBS += $$(pkg-config --libs-only-l libsodium)

instead.

jedisct1 commented 1 month ago

Still, if we want to use only one, LDFLAGS looks way more common that LDLIBS.

Grepping the entire OpenBSD base source code:

$ ug LDLIBS  | wc -l
16

$ ug LDFLAGS | wc -l
2556

It's a bit unexpected that your make command gets LDLIBS but not LDFLAGS. I couldn't reproduce this on any system with gmake, and with BSD make, LDLIBS must be handled explicitly.

What make version do you use?

realconebob commented 1 month ago

What make version do you use?

GNU make 4.3:

GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I wonder if it's a GNU make specific thing

realconebob commented 1 month ago

Hi,

The GNU make manual says that -l parameters should go into LDLIBS but the -L parameters should go to LDFLAGS.

pkg-config has the --libs-only-l and --libs-only-L options to solve this.

So maybe this should be:

LDFLAGS += $$(pkg-config --libs-only-L libsodium)
LDLIBS += $$(pkg-config --libs-only-l libsodium)

instead.

I dropped this into my project and it worked fine, although I have no idea if it would be more or less stable for others