I compiled a program that linked my own compiled zlib dynamic library, but it ended up linking the system's zlib dynamic library.
I compile like this:
root@dggphisprd24174:/test_zlib/myprog# uname -a
Linux dggphisprd24174 4.15.0-163-generic #171-Ubuntu SMP Fri Nov 5 11:55:11 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
root@dggphisprd24174:/test_zlib# echo $LIBRARY_PATH
root@dggphisprd24174:/test_zlib# ls
myprog zlib
root@dggphisprd24174:/test_zlib# cd zlib && ./configure > /dev/null && make > /dev/null && cd ../myprog
root@dggphisprd24174:/test_zlib/myprog# ls
main.c
root@dggphisprd24174:/test_zlib/myprog# gcc -I../zlib -o prog main.c -lz -L../zlib
root@dggphisprd24174:/test_zlib/myprog# ls
main.c prog
root@dggphisprd24174:/test_zlib/myprog# ldd prog
linux-vdso.so.1 (0x00007ffd3190e000)
libachk.so => /lib/libachk.so (0x00007ff936fa5000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ff936a71000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff936680000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff93647c000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff93625d000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007ff936055000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff936e90000)
ldd prog show: libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1.
The share library is not compiled by myself
It confuses me.
Contents of the main.c file
#include <stdlib.h>
#include "zlib.h"
// test link libz.so
int main()
{
char* in = "hello,hello";
char* out = calloc(30, 1);
z_stream strm;
strm.zalloc = 0;
strm.zfree = 0;
strm.next_in = in;
strm.avail_in = 11;
strm.next_out = out;
strm.avail_out = 30;
deflateInit(&strm, 1);
deflate(&strm, Z_FINISH);
deflateEnd(&strm);
}
CodePudding user response:
Try LD_LIBRARY_PATH=../zlib ./examplesh
You could instead add -Wl,-rpath,../zlib
when building examplesh
.