Possible to build a shared library with static link used library?

后端 未结 3 528
傲寒
傲寒 2020-12-29 10:33

I can build a executable with gcc with static link:

gcc -static xxx.c -o xxx

So I can run xxx without any external dependent lib

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 11:09

    There's some neat hackery you can do with Rpath so that a ELF executable or .so will look for its dependent .so files first in the same directory as itself:

    • make a short script echo-rpath consisting of

      echo '-Wl,--rpath=$ORIGIN'

    • add that to your build command line as gcc -o file -lwhatever `echo-rpath ` objects

    (The echo mechanism prevents Make or the shell from eating the $ sign and ensures it gets passed into ld.)

提交回复
热议问题