On Debian bullseye (testing), with these xz/lzma-related packages installed:
ii liblzma-dev:amd64 5.2.4-1+b1 amd64 ii liblzma5:amd64 5.2.4-1+b1 amd64 ii lzma-dev 9.22-2.1 all
Clang generates a working ./a.out
without issue:
$>gunzip < /usr/share/doc/liblzma-dev/examples/02_decompress.c.gz | sed -s 's/extern int/int' | clang -std=c18 -Wall -Wextra -x c -llzma -
However, attempting to use gcc fails:
$>gunzip < /usr/share/doc/liblzma-dev/examples/02_decompress.c.gz | sed -s 's/extern int/int' | gcc -std=c18 -Wall -Wextra -x c -llzma -
/usr/bin/ld: /tmp/ccl8uTG0.o: in function `init_decoder':
:(.text+0x20): undefined reference to `lzma_stream_decoder'
/usr/bin/ld: /tmp/ccl8uTG0.o: in function `decompress':
:(.text+0x1e1): undefined reference to `lzma_code'
/usr/bin/ld: /tmp/ccl8uTG0.o: in fuction `main':
:(.text+0x4e4): undefined reference to `lzma_end'
collect2: error: ld returned 1 exit status
g++ --version
reports 9.3.0-3
clang++ --version
reports 9.0.1-10
(Those compilers are both installed via apt-get
.)
/etc/ld.so.conf.d/x86_64-linux-gnu.conf
contains
# Multiarch support
/usr/local/lib/x86_64-linux-gnu
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
I have checked that /usr/lib/x86_64-linux-gnu/liblzma.so.5.2.4
(and its .so.5
and .so
symlinks, and the associated .a file too) exist.
Furthermore, the output of readelf --symbols /usr/lib/x86_64-linux-gnu/liblzma.so.5.2.4 | grep 'lzma_[sec]'
shows that those three symbols exist (along with many others):
67: 000000000000c720 106 FUNC GLOBAL DEFAULT 13 lzma_stream_decoder@@XZ_5.0 108: 00000000000039d0 701 FUNC GLOBAL DEFAULT 13 lzma_code@@XZ_5.0 117: 0000000000003c90 65 FUNC GLOBAL DEFAULT 13 lzma_end@@XZ_5.0
What am I doing wrong?
Clang puts the -llzma
outside the --as-needed
area of the linker command line:
[…] -L/usr/lib -llzma /tmp/--e00a22.o -lgcc --as-needed -lgcc_s […]
GCC puts it after --as-needed
:
[…] --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 […]
-llzma /tmp/ccAZHRvb.o -lgcc --push-state
As a result, the linker notices that -llzma
is not needed at that point in the command line and drops it. Linking is order-sensitive.
You can fix this by using - -llzma
instead of -llzma -
, as HolyBlackCat suggested.