ARTICLE AD BOX
In the main project there are 3 shared library sub-projects and one binary project. The shared library libb.so depends on liba.so and shared library libd.so depends on libb.so and lastly the binary op depends on libd.so. As you can see there is hierarchy of library dependency. I was of the assumption that each each shared library internally stores information about the paths of indirect dependendcies when -rpath-link=dir flag is used. But this seems incorrect, please find below the error message that I'm getting when building the binary op.
Project Structure
abcd ├── a │ └── lib.cpp ├── b │ └── lib.cpp ├── build.sh ├── c │ └── main.cpp └── d └── lib.cppa/lib.cpp
#include <iostream> extern "C" void print_a() { std::cout << "Hello " << __func__ << std::endl; }b/lib.cpp
#include <iostream> extern "C" void print_a(); extern "C" void print_b() { print_a(); std::cout << "Hello " << __func__ << std::endl; }d/lib.cpp
#include <iostream> extern "C" void print_b(); extern "C" void print_d() { print_b(); std::cout << "Hello " << __func__ << std::endl; }c/main.cpp
#include <iostream> extern "C" void print_d(); int main() { std::cout << "Hello, world! " << std::endl; print_d(); }build.sh
#!/bin/bash build() { cd a || exit g++ -c -Wall -fPIC lib.cpp g++ -fPIC -shared -o liba.so lib.o cd ../b || exit g++ -c -Wall -fPIC lib.cpp g++ -fPIC -shared -o libb.so lib.o -L../a -la cd ../d || exit g++ -c -Wall -fPIC lib.cpp g++ -fPIC -shared -o libd.so lib.o -L../b -Wl,-rpath-link="$(pwd)/../a" -lb cd ../c || exit g++ -c -Wall -fPIE main.cpp g++ -fPIE -o op main.o -L../d -Wl,-rpath-link="$(pwd)/../b" -ld cd ../ } clean() { echo "clean" sudo rm a/lib.o a/liba.so sudo rm b/lib.o b/libb.so sudo rm d/lib.o d/libd.so sudo rm c/main.o /c/op }When I execute source ./build.sh && build, I get the below error
/usr/bin/ld: warning: liba.so, needed by /home/harsha/abcd/c/../b/libb.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: /home/harsha/abcd/c/../b/libb.so: undefined reference to `print_a' collect2: error: ld returned 1 exit statusI was of the assumption that libd.so knows where indirect dependency shared library liba.so is present which is given via -rpath-link="$(pwd)/../a" while building libd.so
