I'm trying to compile a demo which uses zlib, and I think I've done correct set up for LD_LIBRARY_PATH but it does not work with -lz flag, would you please help me find what's the problem?
lisanhu@lisanhu-XPS-15-9550:kseq$ echo $LD_LIBRARY_PATH
/home/lisanhu/mine/repos/zlib/output/lib
lisanhu@lisanhu-XPS-15-9550:kseq$ make
gcc -g -O2 kseq_test.c -o kseq_test -lz
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1
lisanhu@lisanhu-XPS-15-9550:kseq$ gcc -static -o kseq_test kseq_test.o -lz -L/home/lisanhu/mine/repos/zlib/output/lib
lisanhu@lisanhu-XPS-15-9550:kseq$
The problem is that if I use -L
to force searching the folder, it works, if I put that folder in LD_LIBRARY_PATH, it doesn't work. I'm pretty sure I've used export LD_LIBRARY_PATH
but it still doesn't work.
This is annoying because if I need to compile someone else's code that needs it, it's easier to set up a environment variable to search for the libraries.
--------------Update------------
On another server I tried this and it works smoothly
[lisanhu@farber tmp]$ export LD_LIBRARY_PATH=/home/1677/mine/repos/zlib/zlib-1.2.8
[lisanhu@farber tmp]$ ls
gmon.out kseq.h kseq.tar kseq_test kseq_test.c Makefile
[lisanhu@farber tmp]$ make
cc -g -O2 kseq_test.c -o kseq_test -lz
[lisanhu@farber tmp]$ ldd kseq_test
linux-vdso.so.1 => (0x00007fffb01a6000)
libz.so.1 => /home/1677/mine/repos/zlib/zlib-1.2.8/libz.so.1 (0x00007f9556c83000)
libc.so.6 => /lib64/libc.so.6 (0x00000031fa400000)
/lib64/ld-linux-x86-64.so.2 (0x00000031fa000000)
[lisanhu@farber tmp]$
So my configuration may be correct? Any ideas? I've tried it on my laptops with Ubuntu 16.04 and Fedora 25, updated to stable on Feb. 16, 2017. The server I can only tell it's not newly updated. I'm wondering whether there's problem with the newest gcc?
--------------Update------------
I may know what causes the problem. The error message is cannot find -lz
. It seems to treat -lz
as a single file. I find there's both -l
and -z
flag in the usage of ld
while it may convert -lz
to something like that and find we don't have a parameter for both of the flags and then combine them together as a single file parameter?
Not fully sure about my inference but does anyone have any idea of how to handle this case?
--------------Update------------
Full Makefile
all:kseq.h kseq_test.c
$(CC) -g -O2 kseq_test.c -o kseq_test -lz
clean:
rm -f *.o
--------------Update------------
The 3rd section may not be a valid explanation because after appending -v to cc, I've found -lz is successfully passed to the linker