makefilegnu-make

Is it incorrect/undefined behavior when libraries are linked using relative path?


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


Solution

  • 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

    I repeat, however, that these alternatives solve only one of your two problems.