How to build a shared library (.so) without hardcoded full dependency paths?

前端 未结 6 1574
面向向阳花
面向向阳花 2020-12-29 07:37

I need to build two 3rd party shared libraries, so their .so files will be reused by other projects. However, after build one of these libraries contains hardcoded path to a

6条回答
  •  醉梦人生
    2020-12-29 08:07

    -Wl,-rpath,~/deps/A/lib:~/deps/B/lib:~/dev/MyApp/bin
    

    This linker option is responsible for saving the path inside the library. You need somehow to remove this.

    See with ./configure --help if there's some option that could influence this. Another option is to edit manually the makefile and remove this option.

    == edit2 == One more thing

    -L~/deps/A/lib -L~/deps/B/lib ~/deps/A/lib/libA.so ~/deps/B/lib/libB.so
    

    If libA.so and libB.so don't have SONAME, linking them like "~/deps/A/lib/libA.so" will also cause saving the path. Soname is set using -Wl,-soname, linker option when building shared library.

    If soname is set in the shared library, linking it using "~/deps/A/lib/libA.so" form is ok.

    Like Jan mentioned in the comments, the better way is using "-Llibrary/path -llibrary_name" without rpath.

    -L~/deps/A/lib -L~/deps/B/lib -lA -lB
    

提交回复
热议问题