I am building a shared library A which depends on another shared library B present in another directory dir2. When I link shared library B to A with relative path using -L../dir2 -lB it builds fine. But when I do ldd -r A , it says B => not found. This is causing issue when I try to link A to another library C with -L../dir1 -lA -Wl,-rpath-link=../dir2
You have two different problems, associated with two different pieces of software. ldd is showing you an issue associated with the dynamic linker (a.k.a. dynamic loader or program loader). Your compiler is showing you an issue associated with the static linker.
There are multiple solutions to each problem, but one that would solve them both would be @WhiteOwl's suggestion to install libB (and libA too) to one of the system's default library folders, before you attempt to do anything with it. Details are system dependent, but perhaps /usr/local/lib or /usr/local/lib64 would be appropriate.
However, if both libs are part of the same project then it's probably not convenient to install one before building the other. In this case, some of your alternatives are
add the appropriate -L and -l options for libB to the link command for C
-L../dir1 -L../dir2 -lA -lB
or
pass an appropriate -rpath-link option to the (static) linker, paying appropriate attention to its sensitivity to the order of command-line options. I would expect that the -rpath-link would need to appear before -lA on the command line to be effective for locating shared library dependencies of libA. I suspect, however, that -rpath-link will require either an absolute path or an ${ORIGIN}-relative path.
-L../dir1 -Wl,-rpath-link="$(pwd)/../dir2" -lA
Beware that -rpath-link may have unwanted side effects under some circumstances.
I repeat, however, that these alternatives solve only one of your two problems.